digitalmars.D.learn - type of a variable as a mixin-able string?
- Timothee Cour via Digitalmars-d-learn (28/28) Jun 21 2015 Suppose I have:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (17/55) Jun 22 2015 It's possibly not a real Voldemort type, i.e. one that closes
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)
Jun 21 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 }
Jun 22 2015