www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C# style generic interfaces with templates

reply Robby <robby.lansaw gmail.com> writes:
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?
Jan 14 2007
next sibling parent Daniel Keep <daniel.keep+lists gmail.com> writes:
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
Jan 14 2007
prev sibling next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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-----
Jan 14 2007
prev sibling parent reply Bradley Smith <digitalmars-com baysmith.com> writes:
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
Jan 14 2007
parent Robby <robby.lansaw gmail.com> writes:
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

Jan 15 2007