Self-referencing template parameters
Erik Rasmussen
i_am_erik at yahoo.com
Mon Mar 20 05:44:22 PST 2006
Erik Rasmussen wrote:
> 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
Oops. The problem was the "abstract" on the very first line. Bad Java
habit.
Erik
More information about the Digitalmars-d-learn
mailing list