Scope of Mixins

DLearner bmqazwsx123 at gmail.com
Thu Aug 26 17:01:06 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

The object was to take a variable, and do alternative things with 
it depending on (say) whether it was an 'int' or an 'int*'.
Since entirely possible (indeed likely) that operations on 'int' 
invalid or meaningless with 'int*', to me seemed better to find a 
way that at _compile-time_ detected the difference, and only 
generated code valid for the type used.

Originally, there were mixins that only coped with each type.
These work, but a chore to update mixin name as variable type 
changed.
So idea is just one mixin which can compile-time detect variable 
type
and generate appropriate code.
Got it to work, except for this scoping issue...



More information about the Digitalmars-d-learn mailing list