mixin identifiers concept

bauss jj_1337 at live.dk
Mon Feb 7 11:25:26 UTC 2022


On Monday, 7 February 2022 at 10:49:13 UTC, Quirin Schroll wrote:
> On Monday, 31 January 2022 at 07:19:49 UTC, bauss wrote:
>> 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;
>>> }
>>>
>>> writeln(file_boo);
>>> writeln(boo);
>>> writeln(file_foo_info);
>>> ```
>>> 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.
>>
>> I believe a DIP would be necessary, but to be honest I don't 
>> see a reason for this.
>>
>> You can just do this which isn't really much less readable.
>>
>> ```d
>>     static foreach (name; ["boo", "foo"])
>>     {
>>         mixin(`string file_%1$s = 
>> readText("%1$s.txt");`.format(name));
>>     }
>>
>>     writeln(file_boo);
>>     writeln(file_foo);
>> ```
>>
>> And if D ever gets interpolated strings then the whole format 
>> can be removed and it could probably become something like 
>> this:
>>
>> ```d
>>     static foreach (name; ["boo", "foo"])
>>     {
>>         mixin(i`string file_$name = readText("$name.txt");`);
>>     }
>>
>>     writeln(file_boo);
>>     writeln(file_foo);
>> ```
>>
>> So personally I don't see a reason for your suggestion to be 
>> added.
>>
>> I think it's just going to introduce more complexity than it's 
>> worth.
>
> One big disadvantage interpolated strings have is you have to 
> put the full declaration in a string. No big deal for a 
> statement, but if the name of a function depends on 
> compile-time stuff, the whole function is a string.
>
> A much easier solution to this is allowing `mixin()` as an 
> `Identifier` in general:
>
> ```diff
>  Identifier:
>      IdentifierStart
>      IdentifierStart IdentifierChars
> +    MixinExpression
> ```
>
> So, you'd do
>
> ```d
> static foreach(name; ["boo", "foo"])
> {
>     enum string file_name_info = "file_" ~ name ~ "_info";
>     string mixin("file_", name) = readText(name ~ ".txt");
>     string mixin(file_name_info) = readText(name ~ ".meta");
>     auto mixin(name) = mixin(file_name_info).parseInfoFile;
> }
> ```

Yeah, I'm definitely not against mixin working for identifiers.

My example was as a solution if it's never accepted.


More information about the Digitalmars-d mailing list