www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get type name from current class at compile time?

reply Jack <jckj33 gmail.com> writes:
```d
class A
{
	void showMyName()
	{
		writefln("name = [%s]", __traits(identifier, typeof(this)));
	}
}

class K : A { }
```

then

```d
new K().showMyName();
```

output:

name = [A]

I'd like to output `K` and get this identifier at compile time. 
But I'd to do so automatically so rule out something like:

```d
class A
{
	string name = __traits(identifier, typeof(this));

	void showMyName()
	{
		writefln("name = [%s]", name);
	}
}

class K : A
{
	this()
	{
		super.name = __traits(identifier, typeof(this));
	}
}
```
Apr 24 2021
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 24 2021
next sibling parent Jack <jckj33 gmail.com> writes:
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
good one Ali, thanks. Where is this template parameter documented at?
Apr 24 2021
prev sibling parent reply Jack <jckj33 gmail.com> writes:
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 `!()()`,
Apr 24 2021
next sibling parent Jack <jckj33 gmail.com> writes:
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
Apr 24 2021
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 25 April 2021 at 02:26:00 UTC, Jack wrote:
 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 `!()()`,
It works if you call it with `this.doSomething()`: https://run.dlang.io/is/eIygNG
Apr 24 2021
parent reply Jack <jckj33 gmail.com> writes:
On Sunday, 25 April 2021 at 02:45:38 UTC, Paul Backus wrote:
 On Sunday, 25 April 2021 at 02:26:00 UTC, Jack wrote:
 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 `!()()`,
It works if you call it with `this.doSomething()`: https://run.dlang.io/is/eIygNG
that's better, thanks
Apr 24 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 25 April 2021 at 03:45:13 UTC, Jack wrote:
 that's better, thanks
Imporant to remember that any compile time thing will be the static type. If someone does: Base a = new Derived(); a.something(); it will still show up as Base in the this template. The knowledge that it is actually a Derived thing is not there at compile time. (well ok it kind of is, like the compiler's optimizer can still piece it together by following the flow, but it is no longer known to any template after that assignment.)
Apr 25 2021
parent reply Jack <jckj33 gmail.com> writes:
On Sunday, 25 April 2021 at 08:36:51 UTC, Adam D. Ruppe wrote:
 On Sunday, 25 April 2021 at 03:45:13 UTC, Jack wrote:
 that's better, thanks
Imporant to remember that any compile time thing will be the static type. If someone does: Base a = new Derived(); a.something(); it will still show up as Base in the this template.
I find out this later. I give up trying to get this in automatic way at compile time
Apr 25 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 25 April 2021 at 12:54:43 UTC, Jack wrote:
 I find out this later. I give up trying to get this in 
 automatic way at compile time
That's because the type might not be known at compile time at all, it might come from like a plugin loaded at run time and only ever accessed through the interface.
Apr 25 2021