digitalmars.D.learn - Obtaining type and value of a variable named in another variable
- DLearner (7/7) Oct 16 2021 Hi
- Dennis (3/6) Oct 16 2021 `mixin(strVar)`
- DLearner (13/19) Oct 16 2021 ```
- Tejas (14/36) Oct 16 2021 I don't know how to do it with a string, but are you willing to
- Paul Backus (8/20) Oct 16 2021 You can fix this by making `strVar` a [manifest constant][1]:
- Tejas (4/28) Oct 16 2021 Doesn't solve the compile-time only problem though :(
- Paul Backus (20/23) Oct 16 2021 Well, the fundamental issue is that in a language like D that
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
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
On Saturday, 16 October 2021 at 19:29:59 UTC, Dennis wrote:On Saturday, 16 October 2021 at 19:28:04 UTC, DLearner wrote:``` 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".How does one obtain from strVar: 1. The type of fooVar;`typeof(mixin(strVar))`2. The value of fooVar?`mixin(strVar)`
Oct 16 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: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); } ```On Saturday, 16 October 2021 at 19:28:04 UTC, DLearner wrote:``` 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".How does one obtain from strVar: 1. The type of fooVar;`typeof(mixin(strVar))`2. The value of fooVar?`mixin(strVar)`
Oct 16 2021
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
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: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.```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
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