templated function call from interface

Ali Çehreli acehreli at yahoo.com
Sat Nov 16 08:43:27 PST 2013


On 11/16/2013 12:42 AM, rumbu wrote:

 > interface I
 > {
 >      void foo(T)(T t);

Hm? What would the meaning of that declaration be? Would the interface 
be happy with any instantiation of foo?

 > }
 >
 > class C: I
 > {
 > }
 > //bug 1: compiler does not complain about the fact that C doesn't
 > implement foo(), even if C is instantiated somewhere.

Considering that foo() itself is not an implementation, the compiler 
could not complain about the lack a specific instantiation like foo!int 
or foo!MyStruct. Which one is missing? :)

 > void bar(I i)
 > {
 >      i.foo(2);
 > }
 > //bug 2: Error 42: Symbol Undefined _D4main1I10__T3fooTiZ3fooMFiZv (void
 > main.I.foo!(int).foo(int))
 >
 > dmd 2.064.2 windows.
 >
 > I suppose that these two are related.
 > Are these bugs or it's not possible to have templated functions in an
 > interface?

All I can see is that it would be a very flexible interface function: 
"There is this foo() that you can call on this interface but consult 
with the implementing classes whether your specific instance exists." :)

On the other hand, an interface template makes sense to me:

interface I(T)
{
     void foo(T t);
}

That would mean that an I!MyStruct guarantees that it support foo(MyStruct).

Ali



More information about the Digitalmars-d-learn mailing list