DIP idea: q{}-inspired block mixins

Q. Schroll qs.il.paperinik at gmail.com
Wed Jun 10 23:21:25 UTC 2020


On Wednesday, 10 June 2020 at 12:49:47 UTC, Stanislav Blinov 
wrote:
> On Wednesday, 10 June 2020 at 03:03:49 UTC, Q. Schroll wrote:
>
> I like it, but I have some Qs for a Q
>
>> Q: Can I use expressions?
>> A: Instead of a plain identifier, use [ident = expression()].
>
> Why expression()? I.e. why parentheses?

To indicate an evaluation may take place. I thought it were more 
understandable than without.

> Evaluated how many times? Eagerly once? Lazily every time 
> 'ident' is encountered within the {} ? Lazily once, the first 
> time 'ident' is encountered within the {}?

I'd suggest every assignment eagerly once. That's the version 
that seems the least confusing in most cases.

Imagine it like this:

     enum __ident = ident;

or

     enum __ident = expression();

depending on which one is used, and splicing "...", __ident, 
"..." into the parameters to regular mixin().

The same is done in foreach loops to the range expression; in 
foreach (element; range()), (range() is an expression), the range 
expression is evaluated once eagerly. Maybe, if you (or someone 
else) can make a compelling point to add `lazy ident = expr()` 
for the case where it should be evaluated lazily every time it's 
spliced in, I'd put it in the DIP.

Also:

Q: Why brackets?
A: Because any other syntax that came to my mind is either less 
readable or entails code breakage.


More information about the Digitalmars-d mailing list