DIP idea: q{}-inspired block mixins
Q. Schroll
qs.il.paperinik at gmail.com
Fri Nov 6 00:50:16 UTC 2020
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.
More information about the Digitalmars-d
mailing list