Self-referencing template parameters

Erik Rasmussen i_am_erik at yahoo.com
Mon Mar 20 05:36:15 PST 2006


Oskar Linde wrote:
> The problem is that this line contains a recursive instantiation:
> abstract class Animal(T : Animal!(T))
> 
> Animal!(T) has to be instantiated before the compiler knows how to what 
> type it is.
> 
> But if you place a static assert in a place outside what the compiler 
> needs to deduce the type:
> 
> class Animal(T) {
>     this() {
>         static assert(is(T : Animal));
>     }
>     public abstract T[] procreate(T mate);
> }
> 
> Things work as they should.
> 
> 
> /Oskar

Thanks, but...

test.d
---
abstract class Animal(T)
{
   this()
   {
     static assert(is(T : Animal));
   }

   public abstract T[] procreate(T mate);
}

class Monkey : Animal!(Monkey)
{
   public Monkey[] procreate(Monkey mate)
   {
     return new Monkey[2];
   }
}

int main(char[][] args)
{
   new Monkey();
}
---

When I compile, I get:
test.d: variable test.Animal!(Monkey).Animal.this.this abstract cannot 
be applied to variable
test.d:3: variable test.Animal!(Monkey).Animal.this.__result abstract 
cannot be applied to variable

Now what?  Line 3 is the "this()" line.

Erik



More information about the Digitalmars-d-learn mailing list