Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 28, 2009 Template in interface ? | ||||
---|---|---|---|---|
| ||||
Hello, I need to do something like this : /* ---- CODE ---- */ import tango.io.Stdout; interface I { bool cmpType(U)(U val); } class A(T):I { bool cmpType(U)(U val) { static if ( is (U==T) ) return true; else return false; } } void main() { I a = new A!(int); int tInt = 10; double tDouble = 20; float tFloat = 30; a.cmpType(tInt); a.cmpType(tDouble); a.cmpType(tFloat); } /* ---END CODE ---- */ But it's not allow by the compiler when there's no problem when change the line: I a = new A!(int); By A a = new A!(int); :-( Does something have an idea on how to do something like this ? Thanks in advance, Tsalm |
March 01, 2009 Re: Template in interface ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TSalm | TSalm wrote: > Does something have an idea on how to do something like this ? You don't. Templates cannot participate in polymorphism. I've dealt with this in the past and ended up making another class with a template parameter that corresponds to the desired function template parameter... Basically, you change: interface IFoo { void method(T)(T t); } to: interface IFoo(T) { void method(T t); } This only works in some situations. > Thanks in advance, > Tsalm |
March 01, 2009 Re: Template in interface ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | Le Sun, 01 Mar 2009 03:07:16 +0100, Christopher Wright <dhasenan@gmail.com> a écrit:
> TSalm wrote:
>> Does something have an idea on how to do something like this ?
>
> You don't. Templates cannot participate in polymorphism. I've dealt with this in the past and ended up making another class with a template parameter that corresponds to the desired function template parameter...
>
> Basically, you change:
>
> interface IFoo
> {
> void method(T)(T t);
> }
>
> to:
> interface IFoo(T)
> {
> void method(T t);
> }
>
> This only works in some situations.
>
But, in fact, I need to call a "template-method" of a "template-class", for which the calling code doesn't know the type of the "template-class" to call this method.
It's why I tried to use an Interface.
|
March 01, 2009 Re: Template in interface ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TSalm | TSalm wrote: > Le Sun, 01 Mar 2009 03:07:16 +0100, Christopher Wright <dhasenan@gmail.com> a écrit: > >> TSalm wrote: >>> Does something have an idea on how to do something like this ? >> >> You don't. Templates cannot participate in polymorphism. I've dealt with this in the past and ended up making another class with a template parameter that corresponds to the desired function template parameter... >> >> Basically, you change: >> >> interface IFoo >> { >> void method(T)(T t); >> } >> >> to: >> interface IFoo(T) >> { >> void method(T t); >> } >> >> This only works in some situations. >> > But, in fact, I need to call a "template-method" of a "template-class", for which the calling code doesn't know the type of the "template-class" to call this method. > It's why I tried to use an Interface. The remaining way: void methodThatUsesTemplateOnAClass(Class)(Class instance) { instance.method!(SomeArgument)(); } You just can't store these arguments easily. |
Copyright © 1999-2021 by the D Language Foundation