mixin identifiers concept

Nick Treleaven nick at geany.org
Mon Jan 31 12:27:12 UTC 2022


On Saturday, 29 January 2022 at 21:24:09 UTC, mesni wrote:
> ```d
> static foreach(name; ["boo", "foo"]){
>     string file_#name = readText(name~".txt");
>     string file_#name#_info = readText(name~".meta");
>     auto ##name = file_#name#_info.parseInfoFile;
> }

A general feature for this has been suggested several times 
before by various people (initially in 2009!). (I noticed your 
syntax above seems to flip the meanings of the C preprocessor 
operators though, where stringize is `#` and concatenate is 
`##`). The other proposal is to allow `mixin(str)` anywhere an 
identifier is expected. It was mentioned in the `static foreach` 
DIP:
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#mixin-identifiers

The nice thing about re-using string `mixin` syntax for this is 
that (since then) it can take multiple arguments which the 
compiler concatenates. So this would work:

```d
static foreach(n; [1,2,3]) {
     int mixin("foo", n) = n;
}
assert(foo1 == 1);
```

> To make it easier to read/write code in D, I would introduce 
> new mixins. These mixins can only represent identifiers. Also, 
> the advantage of such mixins, the parser and semantic analysis 
> will be able to detect some errors before the code is 
> completely "opened". Also, I think they can speed up 
> compilation.

Yes, parsing would be a bit quicker vs putting the whole 
declaration in a string with CTFE string concatenation. Parsing 
error messages would be much easier to deal with, besides the 
likelihood of errors being lower anyway as the code would be much 
more readable.


More information about the Digitalmars-d mailing list