The state of string interpolation

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Dec 6 21:45:15 UTC 2018


On Thu, Dec 06, 2018 at 09:20:54PM +0000, Mike Franklin via Digitalmars-d wrote:
> On Thursday, 6 December 2018 at 20:00:02 UTC, H. S. Teoh wrote:
> > Now, I'm also thinking about how to minimize the "surface area" of
> > language change in order to make this proposal more likely to be
> > accepted by W&A.
> 
> IMO the one thing that will likely get this across the finish line is
> to generalize it:
> 
> 1) What is the fundamental missing language feature that is preventing
> us from implementing interpolated strings in the library?
> 
> 2) What other use cases besides string interpolation would be enabled
> by adding such a feature?
> 
> If a compelling language feature that has more uses beyond string
> interpolation can be engineered and/or discovered, I think it will
> have a much better chance of being accepted.
[...]

Thanks for stating what I had in mind in a much clearer way than I did.
:D

After thinking about it more, I realize we do have mixin templates that
are able to access symbols in the surrounding scope already. So, at
least in principle, we could already implement string interpolation in
the library, e.g.:

	int i, j;
	string s, t;
	mixin interpolate!"${s} owes ${i} favors and ${j} free tickets to ${t}";
	writeln(interpolateResult);

or something along similar lines.

The only real objection I can see is the ugliness of the syntax, since
currently mixin templates can only be used in statements, rather than
expressions, so to pass multiple interpolated strings to a function,
you'd need a bunch of ugly assignments and temporaries on multiple
lines, thus diminishing the usability of the feature.

If syntactic sugar is the only essential ingredient, then I'm not seeing
a high chance of this proposal being accepted.

But OTOH, making mixin templates usable as expressions, not just
statements, extends beyond mere syntactic niceness. It would allow a
wider application of mixin templates to be usable as smaller chunks of a
larger expression, that otherwise would require onerous amounts of
boilerplate to express.  It's also arguably a natural extension of mixin
templates as an existing language feature (i.e., we already have mixin
template statements, the logical next step is mixin template
expressions). So that could possibly be the angle we should push for.


T

-- 
"Hi." "'Lo."


More information about the Digitalmars-d mailing list