First life-signs of type functions

Stefan Koch uplink.coder at googlemail.com
Tue May 12 14:28:43 UTC 2020


On Wednesday, 6 May 2020 at 11:58:36 UTC, Stefan Koch wrote:
> Hello Guys,
>
> After a bit of fiddling the following code (and only that 
> because I need do partial evaluation of the semantic pass on 
> the function body manually ...)
> Now works on the Talias branch
>
> string F(alias y)
> {
>     return y.stringof;
> }
> static assert(F!(ulong) == "ulong");
> static assert(F!(uint) == "uint");

The following now works:

string[] memberNames(alias T)
{
     string[] result;

     if (is(typeof(T.tupleof)))
     {
         auto memberTuple = T.tupleof; // needs to be auto because 
we can't parse "alias[]" yet.
//        result.length = memberTuple.length; // we don't yet 
exclude this expression from semantic

         // will not be unrolled :)
         foreach(i, f; memberTuple)
         {
//          result[i] = __traits(identifier, f); // because we 
couldn't set the length this wouldn't work
             result ~= __traits(identifier, f); // note how the 
identifier f does not result in "f" ... this is neat but I might 
have to go back on this.
         }
     }

     return result;
}

struct S
{
     int x;
     double y;
     float z;
}

pragma(msg, memberNames(S)); // output is ["x","y","z"]

Note that the bang is not needed.
Because in the end a type function is a regular function.
The call mechanism just works, as long as an implicit cast from 
any type to alias is provided.


More information about the Digitalmars-d mailing list