Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 15, 2007 C# style generic interfaces with templates | ||||
---|---|---|---|---|
| ||||
Newish to D, so bare with me~ I have a few questions surrounding Class Templates and the like if anyone has time. After reading the two pages on templates (lex, and revisited) I haven't seen anything regarding interfaces being templated like classes. However it doesn't produce a lexical error : public interface H(T){ T another(); } public class A(T) : H!(T) { T t; T another(){return t;} void curious(){} } What I'm aiming for is an equivalent to c#'s generic interfaces. So I can remove casting on my interface declarations. However, when I do this : alias A!(int) a; assert(a.another()==0); I get the following error : Error: need 'this' to access member another. So, is there a way where I can template interfaces as I do classes to give a per type based interfaces to the classes? Further on... How do I get the A class instantiated? Is that possible with templated classes? public class A(T) { T t; this(){} } alias A!(int) a; a b = new a(); // doesn't work A! b = new A!(int)();// doesnt either? Am I wrong to think that I can use and approach class templates as if I were using generics in c#? Or am I going about it the wrong way? |
January 15, 2007 Re: C# style generic interfaces with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robby | Running out the door: this works for me with D 1.0: interface H(T) { T another(); } class A(T) : H!(T) { T t; T another() { return t; } void curious() { } } import std.stdio; void main() { alias A!(int) a; a b = new a; A!(int) c = new A!(int); H!(int) d = new A!(int); writefln("b.another: ", b.another); writefln("c.another: ", c.another); writefln("d.another: ", d.another); } -- Daniel |
January 15, 2007 Re: C# style generic interfaces with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robby Attachments: | Robby schrieb am 2007-01-15: > Newish to D, so bare with me~ > > I have a few questions surrounding Class Templates and the like if anyone has time. > > After reading the two pages on templates (lex, and revisited) I haven't seen anything regarding interfaces being templated like classes. However it doesn't produce a lexical error : > > public interface H(T){ > T another(); > } > public class A(T) : H!(T) > { > T t; > T another(){return t;} > void curious(){} > } > > What I'm aiming for is an equivalent to c#'s generic interfaces. So I > can remove casting on my interface declarations. > However, when I do this : > alias A!(int) a; > assert(a.another()==0); > > I get the following error : Error: need 'this' to access member another. "a" is a type, not an instance: # alias A!(int) a; # # int main(){ # a b = new a(); # assert(b.another() == 0); # return 0; # } > So, is there a way where I can template interfaces as I do classes to give a per type based interfaces to the classes? > > Further on... > > How do I get the A class instantiated? Is that possible with templated classes? > > public class A(T) > { > T t; > this(){} > } > > alias A!(int) a; > a b = new a(); // doesn't work > A! b = new A!(int)();// doesnt either? If the above code is at module level it will fail due to "new ..." being a non-constant expression: # alias A!(int) a; # a b; # A!(int) c; # # static this(){ # b = new a(); # c = new A!(int)(); # } Thomas |
January 15, 2007 Re: C# style generic interfaces with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robby | You almost had it right, but made a couple of minor coding mistakes. > public interface H(T){ > T another(); > } > public class A(T) : H!(T) > { > T t; > T another(){return t;} > void curious(){} > } > > alias A!(int) a; > assert(a.another()==0); In this code, "a" is a type not an object. I changed it to: alias A!(int) a; a aObject = new a; assert(aObject.another()==0); > public class A(T) > { > T t; > this(){} > } > > alias A!(int) a; > a b = new a(); // doesn't work > A! b = new A!(int)();// doesnt either? The last line is incorrect. I changed it to: A!(int) b2 = new A!(int)(); Hope that helps. Bradley |
January 15, 2007 Re: C# style generic interfaces with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bradley Smith | Sweet!, now I can port my collection classes to d without casting hell, thanks!
Robby
Bradley Smith wrote:
> You almost had it right, but made a couple of minor coding mistakes.
>
>> public interface H(T){
>> T another();
>> }
>> public class A(T) : H!(T)
>> {
>> T t;
>> T another(){return t;}
>> void curious(){}
>> }
>>
>> alias A!(int) a;
>> assert(a.another()==0);
>
> In this code, "a" is a type not an object. I changed it to:
> alias A!(int) a;
> a aObject = new a;
> assert(aObject.another()==0);
>
>> public class A(T)
>> {
>> T t;
>> this(){}
>> }
>>
>> alias A!(int) a;
>> a b = new a(); // doesn't work
>> A! b = new A!(int)();// doesnt either?
>
> The last line is incorrect. I changed it to:
> A!(int) b2 = new A!(int)();
>
> Hope that helps.
>
> Bradley
|
Copyright © 1999-2021 by the D Language Foundation