www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.meta.Replace using index

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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.
Jul 28
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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.
No time to complete it but here is a busy syntax that may work: template Replace(T) { template From(Src...) { template With(Dst...) { pragma(msg, T); pragma(msg, Src); pragma(msg, Dst); // Place holder, which needs to be implemented. :) import std.meta : AliasSeq; alias With = AliasSeq!int; } } } void main() { Replace!long .From!(int, long, long, float) .With!(byte, char, double, dchar) a; } Ali
Jul 28
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 28
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 28 July 2017 at 22:06:27 UTC, Ali Çehreli wrote:
 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
Thanks!
Jul 28