www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Typefunctions: alias[] can now be transformed to tuple

reply Stefan Koch <uplink.coder googlemail.com> writes:
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
Oct 06 2020
parent reply Nick Treleaven <nick geany.org> writes:
On Tuesday, 6 October 2020 at 09:01:00 UTC, Stefan Koch wrote:
 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 ;)
Is it tricky because functions can't return tuples? Why would it be a bad idea? Just curious. Also should an alias sequence (type tuple) implicitly convert to an alias[]? I expect that would require copying too.
Oct 06 2020
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 6 October 2020 at 13:31:05 UTC, Nick Treleaven wrote:
 On Tuesday, 6 October 2020 at 09:01:00 UTC, Stefan Koch wrote:
 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 ;)
Is it tricky because functions can't return tuples? Why would it be a bad idea? Just curious. Also should an alias sequence (type tuple) implicitly convert to an alias[]? I expect that would require copying too.
Actually it does not require copying. Internally I create a new node and put in a link to an existing sub-tree. auto ae = e.isArrayLiteralExp(); return new TupleExp(e.loc, ae.elements); done :) It's a bad idea because then I would have unrolled foreachs again. And worse the type system would lie.
Oct 06 2020