Thread overview
Passing a type as paramter to super
Jan 04, 2018
Marc
Jan 04, 2018
Marc
Jan 04, 2018
Ali Çehreli
January 04, 2018
For code generation purposes, I'd like to pass a type name to base class. I'm not sure if it's supported, I didn't find anything at documentation for class constructor but it does compile:

> class A {
> 	static {
> 		int a, b;
> 	}
> 
> 	this(T)() {
> 
> 	}
> }

then do something like this:

> class B {
>  this() {
>    super!B;
>  }
>}

but I got the error:

> found ! when expecting ; following statement
January 04, 2018
On Thursday, 4 January 2018 at 19:16:03 UTC, Marc wrote:
> For code generation purposes, I'd like to pass a type name to base class. I'm not sure if it's supported, I didn't find anything at documentation for class constructor but it does compile:
>
>> class A {
>> 	static {
>> 		int a, b;
>> 	}
>> 
>> 	this(T)() {
>> 
>> 	}
>> }
>
> then do something like this:
>
>> class B {
>>  this() {
>>    super!B;
>>  }
>>}
>
> but I got the error:
>
>> found ! when expecting ; following statement

sorry I mean define class b as following:

> class B : A {
>  this() {
>    super!B;
>  }
>}

January 04, 2018
On 01/04/2018 11:16 AM, Marc wrote:
> For code generation purposes, I'd like to pass a type name to base class. I'm not sure if it's supported, I didn't find anything at documentation for class constructor but it does compile:
> 
>> class A {
>>     static {
>>         int a, b;
>>     }
>>
>>     this(T)() {
>>
>>     }
>> }
> 
> then do something like this:
> 
>> class B {
>>  this() {
>>    super!B;
>>  }
>> }
> 
> but I got the error:
> 
>> found ! when expecting ; following statement

Checkout the "this template parameter":

class A {
    static {
        int a, b;
    }

    this(this T)() {
        import std.stdio;
        writeln(T.stringof);
    }
}

class B : A {
    this() {
        super();
    }
}

void main() {
    auto b = new B();
}

Ali
January 04, 2018
On 1/4/18 2:19 PM, Marc wrote:
> On Thursday, 4 January 2018 at 19:16:03 UTC, Marc wrote:
>> For code generation purposes, I'd like to pass a type name to base class. I'm not sure if it's supported, I didn't find anything at documentation for class constructor but it does compile:
>>
>>> class A {
>>>     static {
>>>         int a, b;
>>>     }
>>>
>>>     this(T)() {
>>>
>>>     }
>>> }
>>
>> then do something like this:
>>
>>> class B {
>>>  this() {
>>>    super!B;
>>>  }
>>> }
>>
>> but I got the error:
>>
>>> found ! when expecting ; following statement
> 
> sorry I mean define class b as following:
> 
>> class B : A {
>>  this() {
>>    super!B;
>>  }
>> }
> 

AFAIK, you can't explicitly use template parameters for constructors, it has to be IFTI. Ali has the right idea for capturing the type of this.

But it only goes one level deep, it doesn't give you the full derived type if there are 2 or more levels.

Note, you may want to simply define a separate non-constructor for this purpose, where you have full control.

-Steve