Build an alias array

Alex AJ at gmail.com
Tue Apr 2 03:15:36 UTC 2019


Is there any way to build an alias array at compile time that 
isn't too heavy in resources?

alias arr = Alias!([]);
static foreach(a; A)
{{
     arr.add!a;
}}

and so at the end arr would simply be A, in this case. The reason 
for the double {{}} is because can't build an alias inside since 
the scope may be closed for other reasons.

One can use mixins to create an alias for each case and then take 
the final result if a double scope is not used, but that is 
problematic in some cases and results in a bunch of aliases.

I need to build a corresponding alias array that doesn't have 
certain elements in it, I could use filter but that requires 
duplicating a lot of unnecessary code just to build the sequence.

Filter is defined as

template Filter(alias pred, TList...)
{
     static if (TList.length == 0)
     {
         alias Filter = AliasSeq!();
     }
     else static if (TList.length == 1)
     {
         static if (pred!(TList[0]))
             alias Filter = AliasSeq!(TList[0]);
         else
             alias Filter = AliasSeq!();
     }
     else
     {
         alias Filter =
             AliasSeq!(
                 Filter!(pred, TList[ 0  .. $/2]),
                 Filter!(pred, TList[$/2 ..  $ ]));
     }
}

which takes a list and builds a new list from it using recursion.

I really don't see why we can't do the reverse and add to a 
sequence. Maybe a special aliasArray type needs to be created to 
handle it? It is essentially the same process but in reverse. I 
realize that one case reduces a pre-existing array while the 
other builds it up, but it really shouldn't be a huge problem to 
have both.

After all, we can build up a sequence using unique names so why 
not simply allow masking of the names?

alias arr1 = AliasSeq!(int);
alias arr2 = AliasSeq!(arr1, double);
alias arr3 = AliasSeq!(arr2, double);

etc...

can simply use arr for the entire set and arr really is arr3.




More information about the Digitalmars-d-learn mailing list