www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Obtaining type and value of a variable named in another variable

reply DLearner <bmqazwsx123 gmail.com> writes:
Hi

Suppose string variable strVar has value "fooVar".
fooVar is a valid variable name used elsewhere in the program.

How does one obtain from strVar:
1. The type of fooVar;
2. The value of fooVar?

Best regards
Oct 16 2021
parent reply Dennis <dkorpel gmail.com> writes:
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)`
Oct 16 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
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".
Oct 16 2021
next sibling parent Tejas <notrealemail gmail.com> writes:
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); } ```
Oct 16 2021
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
Oct 16 2021
parent reply Tejas <notrealemail gmail.com> writes:
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.
Oct 16 2021
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 17 October 2021 at 02:42:54 UTC, Tejas wrote:
 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.
Well, the fundamental issue is that in a language like D that compiles to machine code, variable names only exist at compile time. At runtime, the only thing you have is memory addresses (pointers). If you want to look something up by name at runtime, you will have to use a data structure like an associative array: ```d void main() { int[string] vars = ["fooVar": 4]; string strVar = "fooVar"; writeln(typeof(vars[strVar]).stringof); writeln(vars[strVar]); } ``` Of course, in this example, the associative array can only store `int`s. If you want to store multiple types of data, you will need to use a data type that supports runtime polymorphism, like a class, a sum type, or a variant.
Oct 16 2021