DIP 1027---String Interpolation---Community Review Round 1

Adam D. Ruppe destructionator at gmail.com
Thu Dec 12 17:01:01 UTC 2019


On Thursday, 12 December 2019 at 15:41:54 UTC, aliak wrote:
> Except you can actually assign it to a string?

The javascript version is allowed to return whatever it wants. 
The default one returns string but you can do other things with 
it too like return objects or functions or whatever.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

It passes the strings and the original elements, and you can 
actually even request the raw string to do custom escape 
sequences in it (really, the JS lexer treats it as a raw string, 
then there's a function on the object that can do default escape 
sequences for you).

If you wanna talk about taking the best ideas from other 
languages, Javascript's template literals are actually really 
nice. They are so much more than a multi-line string or a way to 
concatenate things.

This is why my proposal transforms the "interpolated string" into 
an object. Making it do the raw string with an escape method is a 
good idea to take too.

Anyway, the object can `alias toString this` so you assign it to 
string and it just works like newbies expect. Templates don't 
trigger alias this but do often use toString so that just works.

But then you can also recognize it specifically in 
functions/templates, store it in variables, etc for further 
processing. My interpolation object can be transformed into a 
("%s", x) tuple. A plain tuple for calls like writeln. Or be 
passed to user functions.

XmlElement e = xml!i"
   <foo>#{bar}</foo>
";


There's a lot of cool things we can do.


More information about the Digitalmars-d mailing list