Obtaining type and value of a variable named in another variable

Tejas notrealemail at gmail.com
Sun Oct 17 02:25:58 UTC 2021


On Saturday, 16 October 2021 at 23:07:22 UTC, DLearner wrote:
> On Saturday, 16 October 2021 at 19:29:59 UTC, Dennis wrote:
>> On Saturday, 16 October 2021 at 19:28:04 UTC, DLearner wrote:
>>> How does one obtain from strVar:
>>> 1. The type of fooVar;
>>
>> `typeof(mixin(strVar))`
>>
>>> 2. The value of fooVar?
>>
>> `mixin(strVar)`
> ```
> 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".

I don't know how to do it with a string, but are you willing to 
do it with an `alias` instead? You won't be able to change it 
later, but then I don't know of any other way.

```d
void main() {

    import std.stdio;

    int fooVar = 4;

    //string strVar;

    alias strVar = fooVar;
    writeln(typeid(strVar));
    writeln(strVar);
}
```



More information about the Digitalmars-d-learn mailing list