www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin/static if issue

reply DLearner <bmqazwsx123 gmail.com> writes:
Please see below:
```
void main() {
    import std.stdio;

    uint TestVar = 5;

    string mxnWrite_Size_t(string VarName) {

       static if (typeof(VarName).stringof == "uint") {

          return `write("` ~ VarName ~ `");`;
       } else {

          return `writeln("Apparently TestVar not a uint");`;	
	  }
    }

    mixin(mxnWrite_Size_t("TestVar"));
}
```
What I expected was the string "TestVar".
What I got was the "Apparently..." error message.
Please, why is this?
Aug 25 2021
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Aug 25, 2021 at 10:16:39PM +0000, DLearner via Digitalmars-d-learn
wrote:
 Please see below:
 ```
 void main() {
    import std.stdio;
 
    uint TestVar = 5;
 
    string mxnWrite_Size_t(string VarName) {
^^^^^^^^^^^^^^ Obviously, VarName is a string. Why would you expect anything else?
       static if (typeof(VarName).stringof == "uint") {
[[...] I think what you meant to write is: static if (typeof(mixin(VarName)).stringof == "uint") { You want the type of the variable named by VarName, not the type of VarName. T -- Many open minds should be closed for repairs. -- K5 user
Aug 25 2021
parent reply DLearner <bmqazwsx123 gmail.com> writes:
On Wednesday, 25 August 2021 at 22:33:00 UTC, H. S. Teoh wrote:
[...}
 I think what you meant to write is:

        static if (typeof(mixin(VarName)).stringof == "uint") {

 You want the type of the variable named by VarName, not the 
 type of VarName.


 T
I understand your reasoning, but: ``` void main() { import std.stdio; uint TestVar = 5; string mxnWrite_Size_t(string VarName) { static if (typeof(mixin(VarName)).stringof == "uint") { return `write("` ~ VarName ~ `");`; } else { return `writeln("Apparently TestVar not a uint");`; } } mixin(mxnWrite_Size_t("TestVar")); } ``` produced Error: variable `VarName` cannot be read at compile time
Aug 25 2021
parent reply jfondren <julian.fondren gmail.com> writes:
On Wednesday, 25 August 2021 at 22:52:23 UTC, DLearner wrote:
 On Wednesday, 25 August 2021 at 22:33:00 UTC, H. S. Teoh wrote:
 [...}
 I think what you meant to write is:

        static if (typeof(mixin(VarName)).stringof == "uint") {

 You want the type of the variable named by VarName, not the 
 type of VarName.


 T
I understand your reasoning, but: ``` void main() { import std.stdio; uint TestVar = 5; string mxnWrite_Size_t(string VarName) { static if (typeof(mixin(VarName)).stringof == "uint") { return `write("` ~ VarName ~ `");`; } else { return `writeln("Apparently TestVar not a uint");`; } } mixin(mxnWrite_Size_t("TestVar")); } ``` produced Error: variable `VarName` cannot be read at compile time
Contrast: ```d void main() { import std.stdio; uint TestVar = 5; string mxnWrite_Size_t(string VarName)() { static if (typeof(mixin(VarName)).stringof == "uint") { return `write("` ~ VarName ~ `");`; } else { return `writeln("Apparently TestVar not a uint");`; } } mixin(mxnWrite_Size_t!"TestVar"); } ``` Output: TestVar
Aug 25 2021
parent DLearner <bmqazwsx123 gmail.com> writes:
On Wednesday, 25 August 2021 at 22:57:23 UTC, jfondren wrote:
 Contrast:
[...]
 ```d
 void main() {
    import std.stdio;

    uint TestVar = 5;

    string mxnWrite_Size_t(string VarName)() {

       static if (typeof(mixin(VarName)).stringof == "uint") {

          return `write("` ~ VarName ~ `");`;
       } else {

          return `writeln("Apparently TestVar not a uint");`;	
 	  }
    }

    mixin(mxnWrite_Size_t!"TestVar");
 }
 ```

 Output: TestVar
Confirm works for me. Thanks!
Aug 25 2021