Search
Dollar identifiers in code-generating constructs
Jul 03
monkyyy
Jul 04
monkyyy

A dollar identifier is an `Identifier` with a `\$` at the end. In a code-generating construct (mixed-in `mixin template` and `static foreach`/`static foreach_reverse`) the `\$` is replaced by a number.

For a loop, it is the index of iteration.

For a mixin template, it is the number of mixin templates mixed in up until this point.

### Example: loop

``````enum xs = ["A", "BC", "DEF"];
static foreach (x; xs)
{
size_t l = x.length; // Error on "BC" and "DEF", already defined.
}
``````
``````enum xs = ["A", "BC", "DEF"];
static foreach (x; xs)
{
size_t l\$ = x.length; // Good: becomes `l0`, `l1`, `l2`
}
``````

### Example: mixin template

``````mixin template mixMeIn(T)
{
alias R = int delegate(T);
}

struct S
{
mixin mixMeIn!int; // Okay
mixin mixMeIn!int; // Error, `R` already defined
}
``````
``````mixin template mixMeIn(T)
{
alias R\$ = int delegate(T);
}

struct S
{
mixin mixMeIn!int; // Okay, defines `R0`
mixin mixMeIn!int; // Okay, defines `R1`
}
``````

On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:

>

A dollar identifier is an `Identifier` with a `\$` at the end. In a code-generating construct (mixed-in `mixin template` and `static foreach`/`static foreach_reverse`) the `\$` is replaced by a number.

[...]

is there a static foreach usecase where you cant use a index?

``````template innate(T,alias discirmintor){
T innate;
}
static foreach(i,A;...){
alias L(int i_:i)=innate(F!A,i);
}
``````

On Wednesday, 3 July 2024 at 13:34:11 UTC, monkyyy wrote:

>

On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:

>

A dollar identifier is an `Identifier` with a `\$` at the end. In a code-generating construct (mixed-in `mixin template` and `static foreach`/`static foreach_reverse`) the `\$` is replaced by a number.

[...]

is there a static foreach usecase where you cant use a index?

``````template innate(T,alias discirmintor){
T innate;
}
static foreach(i,A;...){
alias L(int i_:i)=innate(F!A,i);
}
``````

One can always use the index: `mixin("size_t l", cast(int)i, " = ...")`. This is a lot of boilerplate for something conceptually simple.

On Thursday, 4 July 2024 at 11:13:47 UTC, Quirin Schroll wrote:

>

On Wednesday, 3 July 2024 at 13:34:11 UTC, monkyyy wrote:

>

On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:

>

A dollar identifier is an `Identifier` with a `\$` at the end. In a code-generating construct (mixed-in `mixin template` and `static foreach`/`static foreach_reverse`) the `\$` is replaced by a number.

[...]

is there a static foreach usecase where you cant use a index?

``````template innate(T,alias discirmintor){
T innate;
}
static foreach(i,A;...){
alias L(int i_:i)=innate(F!A,i);
}
``````

One can always use the index: `mixin("size_t l", cast(int)i, " = ...")`. This is a lot of boilerplate for something conceptually simple.

I think you can always also use template specialization and it should be faster to access then mixins and theres a handful of other benefits should as being able to pass them around as an overloadset

Maybe this isnâ€™t worth it, as mixin Identifiers would supersede this.