www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Dollar identifiers in code-generating constructs

reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
A dollar identifier is an 
[*`Identifier`*](https://dlang.org/spec/lex.html#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.

---



```d
enum xs = ["A", "BC", "DEF"];
static foreach (x; xs)
{
     size_t l = x.length; // Error on "BC" and "DEF", already 
defined.
}
```

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



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

struct S
{
     mixin mixMeIn!int; // Okay
     mixin mixMeIn!int; // Error, `R` already defined
}
```

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

struct S
{
     mixin mixMeIn!int; // Okay, defines `R0`
     mixin mixMeIn!int; // Okay, defines `R1`
}
```
Jul 03
next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:
 A dollar identifier is an 
 [*`Identifier`*](https://dlang.org/spec/lex.html#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? ```d template innate(T,alias discirmintor){ T innate; } static foreach(i,A;...){ alias L(int i_:i)=innate(F!A,i); } ```
Jul 03
parent reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
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`*](https://dlang.org/spec/lex.html#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? ```d 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.
Jul 04
parent monkyyy <crazymonkyyy gmail.com> writes:
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`*](https://dlang.org/spec/lex.html#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? ```d 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
Jul 04
prev sibling next sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
Maybe this isn’t worth it, as mixin Identifiers would supersede 
this.
Jul 04
prev sibling next sibling parent Meta <jared771 gmail.com> writes:
On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:
 A dollar identifier is an 
 [*`Identifier`*](https://dlang.org/spec/lex.html#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.

 ---



 ```d
 enum xs = ["A", "BC", "DEF"];
 static foreach (x; xs)
 {
     size_t l = x.length; // Error on "BC" and "DEF", already 
 defined.
 }
 ```

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



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

 struct S
 {
     mixin mixMeIn!int; // Okay
     mixin mixMeIn!int; // Error, `R` already defined
 }
 ```

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

 struct S
 {
     mixin mixMeIn!int; // Okay, defines `R0`
     mixin mixMeIn!int; // Okay, defines `R1`
 }
 ```
This is more or less equivalent to Timon's proposal of __local variables in static foreach loops. See https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#local-declarations.
Jul 08
prev sibling next sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:
 A dollar identifier is an 
 [*`Identifier`*](https://dlang.org/spec/lex.html#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.

 ---



 ```d
 enum xs = ["A", "BC", "DEF"];
 static foreach (x; xs)
 {
     size_t l = x.length; // Error on "BC" and "DEF", already 
 defined.
 }
 ```

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



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

 struct S
 {
     mixin mixMeIn!int; // Okay
     mixin mixMeIn!int; // Error, `R` already defined
 }
 ```

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

 struct S
 {
     mixin mixMeIn!int; // Okay, defines `R0`
     mixin mixMeIn!int; // Okay, defines `R1`
 }
 ```
This is very similar to a proposal by Timon Gehr to add `__local` variables that are local to static foreach bodies: https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#local-declarations
Jul 15
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Monday, 15 July 2024 at 17:50:10 UTC, Meta wrote:
 On Wednesday, 3 July 2024 at 10:25:14 UTC, Quirin Schroll wrote:
 A dollar identifier is an 
 [*`Identifier`*](https://dlang.org/spec/lex.html#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.

 ---



 ```d
 enum xs = ["A", "BC", "DEF"];
 static foreach (x; xs)
 {
     size_t l = x.length; // Error on "BC" and "DEF", already 
 defined.
 }
 ```

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



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

 struct S
 {
     mixin mixMeIn!int; // Okay
     mixin mixMeIn!int; // Error, `R` already defined
 }
 ```

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

 struct S
 {
     mixin mixMeIn!int; // Okay, defines `R0`
     mixin mixMeIn!int; // Okay, defines `R1`
 }
 ```
This is very similar to a proposal by Timon Gehr to add `__local` variables that are local to static foreach bodies: https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#local-declarations
It’s different because my symbols aren’t local. Even if both features could be used for the same end, they end up being quite different. What you do with `R0`, `R1`, etc. is up to you. They’re visible and have predictable names. If you define `R0` elsewhere, the names clash. `__local` on the other hand would hide the symbols outside the loop/template. Whether you want a simple mechanism to generate predictable different names or make symbols basically private is up to your use case.
Jul 16
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
Good idea, but bad syntax, besides, it'll be confusing with 
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1039.md
Jul 16
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Tuesday, 16 July 2024 at 07:03:43 UTC, ryuukk_ wrote:
 Good idea, but bad syntax, besides, it'll be confusing with 
 https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1039.md
How it’ll be confusing? The syntax is very different having the dollar in brackets and on the identifier.
Jul 16