I want to imitate golang's interface in D, to study D's template. I wrote some code: <a href="https://gist.github.com/3123593">https://gist.github.com/3123593</a><div><br></div><div>Now we can write code like golang:</div>
--<br><div>interface IFoo {</div><div>    void foo(int a, string b, float c);</div><div>}</div><div><br></div><div>struct Foo {</div><div>    void foo(int a, string b, float c) {</div><div>        writeln("Foo.foo: ", a, ", ", b, ", ", c);</div>
<div>    }</div><div>}</div><div><br></div><div>struct FooFoo {</div><div>    void foo(int a, string b, float c) {</div><div>        writeln("FooFoo.foo: ", a, ", ", b, ", ", c);</div><div>    }</div>
<div>}</div><div><br></div><div>GoInterface!(IFoo) f = new Foo;</div><div>f.foo(3, "abc", 2.2);</div><div><br></div><div>f = new FooFoo;</div><div>f.foo(5, "def", 7.7);</div>--<div><br><div>It is also very naive, does not support some features, like out/ref parameters, free functions <b>[1]</b> and so on. The biggest problem is downcast not supported. In golang, we can write code like<b>[2]</b>: </div>
<div>--<br>var p IWriter = NewB(10)<br>p2, ok := p.(IReadWriter)<br>--<div><br>Seems [p.(IReadWriter)] dynamically build a virtual table <b>[3]</b>,because the type of "p" is IWriter, it is *smaller* than IReadWriter, the cast operation must search methods and build vtbl at run time.<br>
<br>In D, GoInterface(T).opAssign!(V)(V v) can build a rich runtime information to *V* if we need. But if *V* is interface or base class, the type information not complete. So, seems like I need runtime reflection? and how can I do this in D? I did not find any useful information in the TypeInfo*.</div>
<div><br></div><div>------</div><div>[1] free functions support, e.g.</div><div>--</div><div><div>interface IFoo {</div><div>    void foo(int a, string b, float c);</div><div>}</div></div><div>void foo(int self, int a, string b, float c) {</div>
<div>    writefln("...");</div><div>}</div><div><br></div><div>GoInterface!(int) p = 1;</div><div>p.foo(4, "ccc", 6.6);</div><div>--</div><div><span style="font-family:Arial;font-size:12px;white-space:pre-wrap">In theory no problem.</span></div>
<div><br></div><div>[2] example from <a href="https://github.com/xushiwei/gobook/blob/master/dive-into/interface/03/interface.go">https://github.com/xushiwei/gobook/blob/master/dive-into/interface/03/interface.go</a></div>
<div>[3] /path/of/go/src/pkg/runtime/iface.c: static Itab* itab(InterfaceType *inter, Type *type, int32 canfail)</div></div></div><div><br></div><div><br></div><div>Best regards,</div><div><br></div><div>-- Li Jie</div>