C# style generic interfaces with templates
Thomas Kuehne
thomas-dloop at kuehne.cn
Sun Jan 14 22:25:04 PST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
-----BEGIN PGP SIGNATURE-----
iD8DBQFFqyrjLK5blCcjpWoRApH8AJ9QBjrMZnsy351iNeg+U8BR7N2gpQCcCLYp
XB7367b8YV5et178xFwQGHg=
=txba
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list