Typefunctions: alias[] can now be transformed to tuple

Stefan Koch uplink.coder at googlemail.com
Tue Oct 6 09:01:00 UTC 2020


Good day,

I am not sure if it was visible in the examples that I posted.
But a function returning an alias[] never actually return a tuple.
And if you think about it, you will realize that, if it did, it 
would be dishonest about the return type.
So how can we transform an alias[] back into a tuple once we are 
done with filtering and all that?

It's easy.
You use the .tupleof property.

The latest version of talias_master[0] will actually allow you to 
transform a ctfe-knwon enum alias[] into a TupleExp.

I always intended to have type function returns transform 
themselves automatically into tuples when they are leaving the 
last level of CTFE calls, but that's not only tricky to 
implement, that's also a rather bad idea ;)

The syntax is preliminary of course.
And you can feel free to make suggestions.

Here is the code which you can try out right now!

Cheers!
---
auto makeAliasArray(alias[] types ...)
{
     return types;
}

enum basic_types = makeAliasArray(bool, ubyte, char, byte, 
ushort, wchar, short, uint, dchar, int, ulong, long);
enum mangles = () {
     string result;
     // alias does not expose the mangleof (since the type alias 
itself doesn't have a mangle)
     // so the only way to get at the mangle of the type held in 
the alias variable
     // is to transform it into a real type again, using the 
tupleof property.
     foreach(btype; basic_types.tupleof)
     {
         result ~= "type: " ~ btype.stringof ~ " mangle: '" ~ 
btype.mangleof ~ "'" ~ '\n';
     }
     return result;
} ();

pragma(msg, mangles);
/+ output:
type: bool mangle: 'b'
type: ubyte mangle: 'h'
type: char mangle: 'a'
type: byte mangle: 'g'
type: ushort mangle: 't'
type: wchar mangle: 'u'
type: short mangle: 's'
type: uint mangle: 'k'
type: dchar mangle: 'w'
type: int mangle: 'i'
type: ulong mangle: 'm'
type: long mangle: 'l'
+/
---

[0] https://github.com/UplinkCoder/dmd/tree/talias_master


More information about the Digitalmars-d mailing list