Trying to get type and name at compile time
Edwin van Leeuwen via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 24 09:22:46 PDT 2016
On Tuesday, 24 May 2016 at 15:09:43 UTC, Adam D. Ruppe wrote:
> On Tuesday, 24 May 2016 at 15:01:33 UTC, Edwin van Leeuwen
> wrote:
>> // I expected AliasSeq!(double,"x")???
>> pragma(msg,test); // tuple((double), "x")
>
> What Phobos calls AliasSeq is called tuple inside the compiler.
> They are the same thing, just different names.
That's what I assumed at first.. So why does the following fail
with: cannot interpret double at compile time? I assumed
staticMap would automatically flatten the resulting AliasSeqs.
```
import std.meta : AliasSeq, ApplyLeft, staticMap;
struct Point { double x; double y; }
template addType(T,alias name)
{
alias addType = AliasSeq!( typeof(__traits(getMember, Point,
name)),
name );
}
alias test3 = addType!( Point, "x" );
// I expected AliasSeq!(double,"x")???
pragma(msg,test3); // tuple((double), "x")
//static assert(is(test == AliasSeq!(double,"x")));
alias ts = AliasSeq!("x","y");
alias addTypeP = ApplyLeft!(addType,Point);
alias mapped = staticMap!(addTypeP,ts);
pragma(msg,mapped);
void main() {
}
```
Looking at it now, I guess it is because staticMap does not work
with alias values, only with actual type lists. Is that correct?
Any ideas on how to do this?
>
>> static assert(is(test == AliasSeq!(double,"x")));
>
> AliasSeq is not comparable as a type. You can test the
> individual pieces of it (`is(test[0] == double) && test[1] ==
> "x"`) or wrap it in a struct or something.
I thought so, but a lot of the documentation does seem to compare
it (see the example here):
https://dlang.org/library/std/meta/static_map.html
More information about the Digitalmars-d-learn
mailing list