DIP idea: q{}-inspired block mixins
Patrick Schluter
Patrick.Schluter at bbox.fr
Fri Nov 6 07:33:26 UTC 2020
On Friday, 6 November 2020 at 00:50:16 UTC, Q. Schroll wrote:
> On Thursday, 5 November 2020 at 12:51:38 UTC, Jacob Carlborg
> wrote:
>> Isn't it quite rare to have the mixin location and the string,
>> to be mixed in, at the same place?
>
> I'd say mixin(generate(args)) and mixin("..pattern..", hole,
> "..pattern..") are currently the two standard uses of string
> mixins. While the first is perfectly fine, the latter lacks
> readability greatly, since **code to be read is in string
> literals** where it shouldn't be. In the first use-case,
> generate may stitch together the generated code in a way that
> might be hard to follow, but that's hard to improve. My
> proposal makes code, well, code. Interpolated strings don't
> help that either. Interpolated strings' mission is to generate
> strings which has its merits and use-cases well beyond string
> mixins.
>
> To convince myself I've just run
>
> grep 'mixin(.*"' *.d
>
> and
>
> grep 'mixin.*q{' *.d
>
> over Phobos (std/) and got 169 and 10 results respectively.
> (This search is surface-level but finds good candidates.) I
> reviewed every one of them manually and found 133 of 169 and 8
> of 10 would probably have been written with my proposed syntax.
> Since Phobos is a meta-programming heavy library, I'd consider
> it a representative target, as mixin is without doubt a
> meta-programming tool. To be fair, 78 of the 133 matches follow
> one of the operator mixin patterns ("lhs" ~ op ~ "rhs" or
> op~"value") which is probably the most common and obvious
> use-case.
I like your DIP. It's one of those simple syntax changes that
have a huge effect on readability without introducing complex
concepts. It is also not that uncommon intellectual concept. It
is similar to a with(e) {} or catch(e) {}.
People who object to your DIP for "difficulty to make the link
between expression in [] and the statements" reasons, should be
high up in arms against with statement, as there the readability
is worse as there is not even a hint where the head expression
was omitted.
You should add these statistics to the dip, to bring home the
point. Choose 1 or 2 (or 3) specially juicy examples from that
list showing the undeniable readability advantage it brings.
More information about the Digitalmars-d
mailing list