Oh, forgot to re-add the unsafeaddr when returning, I had it removed to test
the speed difference. The {.global.} variable is instantiated once for each
type the generic proc is instantiated with, I think that is a way to get around
having to call implementInterface.
usage with macro then looks like this:
defInterface Foo:
proc bar(this: Foo): int
type
A = object
B = object
x: int
proc bar(this: ptr A): int = 3
proc bar(this: ptr B): int = this.x
var
a = A()
b = B(x: 5)
a_i = a.toFoo()
b_i = b.toFoo()
collection = @[a_i, b_i]
for entry in collection:
echo entry.bar()
On my first try I screwed up the vtable for procs with additional arguments but
I think I fixed that. Macro ended up being very similar to yours, though.