April 17, 2018 get literal symbol name without base class/struct as string | ||||
---|---|---|---|---|
| ||||
give structs like this: struct A { int a = 10; string s = "haha"; } struct B { A aDetails; } I'd like to do this and store that symbol name as string (my goal is store the member name); string memberName = magic(B.aDetails.s); writeln(memberName); // otuput "aDetails.s" how can I do that? closet I got was: template nameof(alias S) { import std.array : split, join; import std.traits; pragma(msg, fullyQualifiedName!S); pragma(msg, "stringof = " ~ S.stringof); enum parts = fullyQualifiedName!S.split("."); enum nameof = parts[1 .. $].join("."); } but neither fullyQualifiedName nor stringof give the symbol in the way I need. |
April 19, 2018 Re: get literal symbol name without base class/struct as string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dr.No | On Tuesday, 17 April 2018 at 21:45:45 UTC, Dr.No wrote:
> give structs like this:
>
> struct A
> {
> int a = 10;
> string s = "haha";
> }
>
> struct B
> {
> A aDetails;
> }
>
> but neither fullyQualifiedName nor stringof give the symbol in the way I need.
I'd hint you towards
import std.traits;
pragma(msg, FieldNameTuple!B);
pragma(msg, FieldNameTuple!(Fields!B[0]));
|
Copyright © 1999-2021 by the D Language Foundation