Obtaining type and value of a variable named in another variable

Tejas notrealemail at gmail.com
Sun Oct 17 02:42:54 UTC 2021


On Sunday, 17 October 2021 at 02:31:04 UTC, Paul Backus wrote:
> On Saturday, 16 October 2021 at 23:07:22 UTC, DLearner wrote:
>> ```d
>> void main() {
>>
>>    import std.stdio;
>>
>>    int fooVar = 4;
>>
>>    string strVar;
>>
>>    strVar = "fooVar";
>>    writeln(typeof(mixin(strVar)));
>>    writeln(mixin(strVar));
>> }
>> ```
>> Failed with 2x "Error: variable `strVar` cannot be read at 
>> compile time".
>
> You can fix this by making `strVar` a [manifest constant][1]:
>
> ```d
> enum strVar = "fooVar";
> writeln(typeof(mixin(strVar)).stringof);
> writeln(mixin(strVar));
> ```
>
> [1]: https://dlang.org/spec/enum.html#manifest_constants

Doesn't solve the compile-time only problem though :(
The only solution I can think of is something with `variant`s, 
but it'll be messy.


More information about the Digitalmars-d-learn mailing list