Scope of Mixins

DLearner bmqazwsx123 at gmail.com
Thu Aug 26 19:31:54 UTC 2021


On Thursday, 26 August 2021 at 16:28:22 UTC, Adam D Ruppe wrote:
> On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote:
>> Please confirm that mixins of format:
>
> You really shouldn't use string mixins like this at all. If you 
> want to work with a variable, pass the variable itself as an 
> argument to the function and use it with regular code instead 
> of passing names as strings.
>
> void do_something(alias v)() {
>    // use v like a normal variable
> }
>
> int a;
> do_someting!a; // pass the variable a as an alias so you can 
> use it inside

Thank you for your suggestion.
For the record, the code below behaves as expected.
```
void main() {

    int  VarInt;
    int* VarIntPtr;
    double VarDbl;

    do_something!VarInt;
    do_something!VarIntPtr;
    do_something!VarDbl;
}

void do_something(alias v)() {

    import std.stdio;

    if (typeof(v).stringof == "int" ) {

       writeln("int var detected");
    } else if (typeof(v).stringof == "int*") {

       writeln("int* var detected");
    } else {

       writeln("Unrecognised type");
    }
}
```


More information about the Digitalmars-d-learn mailing list