Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 28, 2017 std.meta.Replace using index | ||||
---|---|---|---|---|
| ||||
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. |
July 28, 2017 Re: std.meta.Replace using index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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
|
July 28, 2017 Re: std.meta.Replace using index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
July 29, 2017 Re: std.meta.Replace using index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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!
|
Copyright © 1999-2021 by the D Language Foundation