Better string mixins
Andre Pany
andre at s-e-a-p.de
Fri Dec 28 11:29:04 UTC 2018
On Friday, 28 December 2018 at 05:55:30 UTC, Michelle Long wrote:
> The problem with mixing in code is that one has to manually
> build code strings like:
>
> static foreach(k; X)
> mixin("int Var"~k~" = "~k~";);
>
> which is error prone, ugly, time consuming, and harder to read.
>
> Instead, I propose a simple extension:
>
> static foreach(k; X)
> mixin("int Var at k = @k;");
>
>
> Here the @ simple, or whatever symbol would be best, takes the
> variable and inserts it from the outer scope as a string,
> converting it to a string if necessary.
>
> It can only be used on such CT variables so errors should not
> exist.
>
> If necessary, maybe
>
> static foreach(k; X)
> @mixin("int Var at k = @k;");
>
> or
>
> static foreach(k; X)
> mixin(@"int Var at k = @k;");
>
> could be used.
>
>
> This avoids having to do shit like
>
> mixin("foo("~x~", "~y~", "~z~");");
>
> so one has
>
> mixin("foo(@x, at y, at z);");
>
> which translates almost directly in to standard code(one can
> simply strip the @'s).
As far as I understand you propose a string interpolation syntax
for string mixins only.
There is currently a DIP for adding general string interpolation
to the language. Also there is a library solution available for
string interpolation.
With string interpolation your use case will become quite
readable and less error prone.
When the DIP is in a good shape there might be a chance it is
added to the language. I just found a post of Walter where he
express his open mind for this topic.
Kind regards
Andre
More information about the Digitalmars-d
mailing list