www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Build an alias array

reply Alex <AJ gmail.com> writes:
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.
Apr 01
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 2 April 2019 at 03:15:36 UTC, Alex wrote:
 Is there any way to build an alias array at compile time that 
 isn't too heavy in resources?
 {...}
Hi Alex, I agree that there should be a way to do that. As soon as newCTFE is a releasable state, I'll work on that again :) I'd be intereseted in your usecases, so I can asses the requirements better. Thanks in advance! - Stefan
Apr 02
parent Alex <AJ gmail.com> writes:
On Tuesday, 2 April 2019 at 07:47:29 UTC, Stefan Koch wrote:
 On Tuesday, 2 April 2019 at 03:15:36 UTC, Alex wrote:
 Is there any way to build an alias array at compile time that 
 isn't too heavy in resources?
 {...}
Hi Alex, I agree that there should be a way to do that. As soon as newCTFE is a releasable state, I'll work on that again :) I'd be intereseted in your usecases, so I can asses the requirements better. Thanks in advance! - Stefan
I'm just trying to filter out certain types from a static foreach to use later that are not used immediately. I usually need such a way to build an sequence though. static foreach(a; Seq) { static if (is(a == S)) { } else arr.Add(a); } The idea is that I can then do static foreach(a; arr) { } to get the remaining elements. Sometimes it's just building up up a sequence in some order for some reason such as collecting types and making decisions between those types. Also, sometimes I want to create a hierarchy like a tree but nodes are sequences. These things are easy to do at RT but nearly impossible at CT. With CTFE they can sorta be done but usually one still needs some way to build a sequence. I mean, one can easily do this by using different names as I said: alias arr1 = AliasSeq!(T1); alias arr2 = AliasSeq!(arr1, T2); alias arrnp1 = AliasSeq!(arrn, Tn); but it's difficult to know the final name(well, one can use a mixin) and it has problems with scoping on static if's. The only point of doing it is because we can't reassign to an alias. alias arr = AliasSeq!Empty; static foreach(Tk; S) {{ alias arr = AliasSeq!(arr, Tk); }} Then arr == S after the loop. would be ideal.
Apr 02