www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - take symbol as parameter

reply Marc <jckj33 gmail.com> writes:
how do I take a symbol as parameter?

for example:

 template nameof(alias S) {
	import std.array : split;
	enum nameof = S.stringof.split(".")[$-1];
}
Works fine for say a enum member such nameof!(myEnum.X) but this:
	struct S { int v; }
	S s;
	writefln(nameof!(s.v)); // should return "v"
return the following error:
 Error: template instance nameof!(v) cannot use local 'v' as 
 parameter to > non-global template nameof(alias S)
Dec 30 2017
parent reply rjframe <dlang ryanjframe.com> writes:
On Sat, 30 Dec 2017 13:07:49 +0000, Marc wrote:

 how do I take a symbol as parameter?
 
 for example:
 
 template nameof(alias S) {
	import std.array : split;
	enum nameof = S.stringof.split(".")[$-1];
}
Works fine for say a enum member such nameof!(myEnum.X) but this:
	struct S { int v; }
	S s;
	writefln(nameof!(s.v)); // should return "v"
return the following error:
 Error: template instance nameof!(v) cannot use local 'v' as parameter
 to > non-global template nameof(alias S)
You can use the name of the struct rather than the instance. writefln(nameof!(S.v));
Dec 30 2017
next sibling parent reply Marc <jckj33 gmail.com> writes:
On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:
 On Sat, 30 Dec 2017 13:07:49 +0000, Marc wrote:

 how do I take a symbol as parameter?
 
 for example:
 
 template nameof(alias S) {
	import std.array : split;
	enum nameof = S.stringof.split(".")[$-1];
}
Works fine for say a enum member such nameof!(myEnum.X) but this:
	struct S { int v; }
	S s;
	writefln(nameof!(s.v)); // should return "v"
return the following error:
 Error: template instance nameof!(v) cannot use local 'v' as 
 parameter
 to > non-global template nameof(alias S)
You can use the name of the struct rather than the instance. writefln(nameof!(S.v));
it doesn't work for me:
 Error: template instance nameof!(v) cannot use local 'v' as 
 parameter to non-global template nameof(alias S)
Dec 31 2017
parent Marc <jckj33 gmail.com> writes:
On Sunday, 31 December 2017 at 22:50:12 UTC, Marc wrote:
 On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:
 On Sat, 30 Dec 2017 13:07:49 +0000, Marc wrote:

 how do I take a symbol as parameter?
 
 for example:
 
[...]
Works fine for say a enum member such nameof!(myEnum.X) but this:
[...]
return the following error:
 [...]
You can use the name of the struct rather than the instance. writefln(nameof!(S.v));
it doesn't work for me:
 Error: template instance nameof!(v) cannot use local 'v' as 
 parameter to non-global template nameof(alias S)
Put it at global scope. Worked fine.
Dec 31 2017
prev sibling parent Marc <jckj33 gmail.com> writes:
On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote:
 On Sat, 30 Dec 2017 13:07:49 +0000, Marc wrote:

 how do I take a symbol as parameter?
 
 for example:
 
 template nameof(alias S) {
	import std.array : split;
	enum nameof = S.stringof.split(".")[$-1];
}
Works fine for say a enum member such nameof!(myEnum.X) but this:
	struct S { int v; }
	S s;
	writefln(nameof!(s.v)); // should return "v"
return the following error:
 Error: template instance nameof!(v) cannot use local 'v' as 
 parameter
 to > non-global template nameof(alias S)
You can use the name of the struct rather than the instance. writefln(nameof!(S.v));
How do I make it work when the symbol is defiend as following:
 class C { int a() { return 0; }}
call to nameof!(C.a) give compiler error:
 Error: need 'this' for 'a' of type 'int()'
 template instance foo.nameof!(a) error instantiating
Dec 31 2017