std.meta.Replace using index

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 28 15:06:27 PDT 2017


On 07/28/2017 12:04 PM, Ali Çehreli wrote:
> On 07/28/2017 01:22 AM, Nicholas Wilson wrote:
>> Hi
>>
>> I want to replace each occurrence of a particular type in an AliasSeq
>> with a type from another AliasSeq (the both have the same length) with
>> the corresponding index
>>
>> i.e. (int long long float) (byte char double dchar)  replacing long
>> should yield (int char double float) std.meta.Replace would see to do
>> the trick except the lambda depends in the index and I'm not sure how to
>> pass that.
>

I think it works:

template replace(T) {
     template inside(Src...) {
         template from(Dst...) {
             import std.meta;
             enum f = staticIndexOf!(T, Src);
             static if (f == -1) {
                 alias from = Src;
             } else {
                 alias from = AliasSeq!(Src[0 .. f], Dst[f], 
inside!(Src[f + 1 .. $]).from!(Dst[f + 1 .. $]));
             }
         }
     }
}

unittest {
     import std.meta : AliasSeq;
     replace!long
         .inside!(long, int, long, long, float, long)
         .from!(int, byte, char, double, dchar, real) a;
     static assert(is (typeof(a) == AliasSeq!(int, int, char, double, 
float, real)));
}

void main() {
}

Ali



More information about the Digitalmars-d-learn mailing list