type of a variable as a mixin-able string?
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 22 01:42:46 PDT 2015
On Sunday, 21 June 2015 at 23:25:47 UTC, Timothee Cour wrote:
> Suppose I have:
>
> import std.range;
> auto a=iota(complex_expr_returning_3());
>
> I'd like to have a function/trait/template/compiler magic that
> takes
> variable a and generates a string that can be mixed in to
> represent the
> type of a. The difficulty is that typeid(a).to!string doesn't
> work for
> Voldermort types:
> typeid(a).to!string produces "std.range.iota!(int,
> int).iota.Result", which
> is not very useful as it can't be mixed in a program, being a
> voldemort
> type, eg:
> mixin("std.range.iota!(int, int).iota.Result a;"); //compile
> error
>
> However, the compiler could help reduce it to something as
> simple as
> possible, eg:
> "typeof(iota(int.init))"
> here this would work:
>
> mixin("typeof(iota(int.init)) a;"); //works
>
> Is it possible to do that in a generic way, such that it hides
> as much as
> possible the details of the expression (here,
> "complex_expr_returning_3()"
> should be simplified to int.init)
>
> Or is there another trick I could use to instantiate a variable
> with same type as a?
>
>
> NOTE:
> typeof(a) a2;
> works even though
> http://www.drdobbs.com/cpp/voldemort-types-in-d/232901591?pgno=2 says it
> won't work, "Sorry, that won't work, the compiler will not
> allow a
> Voldemort Type to be instantiated outside of its scope "
> (although that's doesn't help with my original problem)
It's possibly not a real Voldemort type, i.e. one that closes
over a local variable and therefore needs a context pointer. DMD
correctly rejects this code:
auto foo(int a) {
struct S {
int bar() {
return a;
}
}
return S();
}
void main() {
auto a = foo(42);
typeof(a) b; // Error: cannot access frame pointer of
xx.foo.S
}
More information about the Digitalmars-d-learn
mailing list