get type name from current class at compile time?

Jack jckj33 at gmail.com
Sun Apr 25 02:32:05 UTC 2021


On Sunday, 25 April 2021 at 02:26:00 UTC, Jack wrote:
> On Sunday, 25 April 2021 at 02:02:47 UTC, Ali Çehreli wrote:
>> On 4/24/21 6:50 PM, Jack wrote:
>>> I'd like to output `K` and get this identifier at compile 
>>> time.
>>
>> This is solved by the "this template parameter":
>>
>> import std.stdio;
>>
>> class A {
>>   void showMyName(this T)() {
>>     writefln("name = [%s]", __traits(identifier, T));
>>   }
>> }
>>
>> class K : A { }
>>
>> void main() {
>>   new K().showMyName();
>> }
>>
>> Ali
>
> doesn't this work when called from member in a derived class?
>
> ```d
>
> class A
> {
> 	void doSomething(this T)()
> 	{
> 		writefln("name = [%s]", __traits(identifier, T));
> 	}
> }
>
> class K : A
> {
> 	void baa()
> 	{
> 		doSomething();
> 	}
> }
> ```
>
> result in the error:
>
> Error: template `foo.A.doSomething` cannot deduce function from 
> argument types `!()()`,

I can call doSomething!(typeof(this)), I can live with that


More information about the Digitalmars-d-learn mailing list