The state of string interpolation

Paul Backus snarwin at gmail.com
Thu Dec 6 22:13:30 UTC 2018


On Thursday, 6 December 2018 at 21:32:22 UTC, H. S. Teoh wrote:
> Ah, so mixin templates *can* access symbols from the containing 
> scope. That's nice... so actually, we can already implement 
> string interpolation in the library.  The only complaint is the 
> ugly syntax required `mixin blah!"abc ${def}"` instead of 
> simply `blah!"abc ${def}"` or `blah("abc ${def}")`.

Library versions have existed for a while now. See for example 
`interp` from the scriptlike package on DUB [1], and Marler's own 
library version [2] linked in the original Github discussion.

[1] 
https://github.com/Abscissa/scriptlike/blob/v0.10.2/README.md#string-interpolation
[2] https://github.com/dlang/phobos/pull/6339

> I'm in favor of making the syntax less ugly, but I fear 
> Andrei's objection is going to be that (1) this is already 
> possible without a language change, and (2) the proposed 
> language change would effectively only add syntactic sugar, 
> which is unlikely to be enough justification for doing it.

Unless D someday adds AST macros, new syntax is always going to 
require language changes, so I don't think the fact that this is 
"only" a syntax change should disqualify it from consideration.

Whether or not the difference between 
`writeln(mixin(interp!"..."))` and `writeln(i"...")` is 
significant enough to be worth it is, of course, a value 
judgement, but given that string interpolation has a proven 
track-record in other languages, it's a 100% backwards-compatible 
change, and the implementation work has already been done, I 
think it stands a decent chance of being accepted. The most 
direct comparison I can find is DIP 1009, which added syntactic 
sugar for contracts, and was accepted "without objection."


More information about the Digitalmars-d mailing list