tuples and names

Don Clugston dac at nospam.com.au
Fri Nov 17 00:56:04 PST 2006


Kevin Bealer wrote:
> Sorry - that should be:
>       foreach(int i, auto A; z.tupleof) {
>           writefln("<%s>%s</%s>", name[i], A, name[i]);
>       }
> 
> I'm thinking that .fieldnamesof would return a tuple of char[] objects, and maybe
> there would be a .methodnames of that returns method names (or signatures?) to
> allow searching by name for delegate binding purposes.

I was trying to do exactly this, with the applications you mention. I 
already have a module meta.NameOf, which given any alias, returns the name.

eg. this works:
----------
module mymodule;
import meta.NameOf;

struct S { int x; int y; }

void main()
{
    int [] x;
    S s;
    static assert(symbolnameof!(x) == "x");
    static assert(prettynameof!(x) == "int [] mymodule.main.x");
    static assert(prettynameof!(s) ==
            "struct mymodule.S mymodule.main.s");
}
----------
Unfortunately, .tupleof can't return aliases, and although alias tuples 
exist, you can't index or slice them.
If S.tupleof returned aliases of each of its members,
and if it was legal to index an alias tuple, we'd be done.

IE, we already have:

s.tupleof --> returns an expression tuple
typeof(s.tupleof) --> returns a type tuple
typeof(S.tupleof) --> returns a type tuple

and we'd have:

S.tupleof --> returns an alias tuple (currently doesn't compile)
typeof(s).tupleof --> returns an alias tuple (currently doesn't compile)
typeof(typeof(s).tupleof) --> returns a type tuple (currently doesn't 
compile)

and
symbolnameof!(S.tupleof[0]) == "x"

Now arguably we'd be better off with built-in .nameof properties for any 
alias, rather than my library function, but the concept is unchanged.

Oh, and add another property
.methodtupleof
for all the member functions, which behaves exactly the same as 
.tupleof, allow both of them to apply to modules as well as structs and 
classes -- and then compile-time reflection becomes quite comprehensive.



More information about the Digitalmars-d mailing list