The state of string interpolation

Jonathan Marler johnnymarler at gmail.com
Fri Dec 7 15:05:07 UTC 2018


On Friday, 7 December 2018 at 14:40:05 UTC, Jonathan Marler wrote:
> On Friday, 7 December 2018 at 07:53:14 UTC, Mike Franklin wrote:
>> On Friday, 7 December 2018 at 01:47:13 UTC, H. S. Teoh wrote:
>>
>>> And yes, `mixin interp!"..."` is horribly verbose. But 
>>> ostensibly we could abbreviate it to something like `mixin 
>>> i!"..."`.
>>
>> Perhaps what's needed is a different mixin operator that is 
>> not verbose and significantly distinguishes it from the 
>> template instantiation operator.
>>
>> mixin i!"...";  // instead of this...  (too verbose)
>> i!"...";        // or this... (looks too much like a template 
>> instantiation)
>> i#"...";        // we create a new mixin operator `#`
>>
>> Mike
>
> An interesting idea.
>
> Maybe id#(...) lowers to mixin(id(...)) Or something.
>
> Keep in mind though, that interpolated strings implemented 
> using this will have the disadvantages of mixin code, bad error 
> messages, hard to debug and a hit to compile time performance. 
> Plus you still need the extra import. Even if we could 
> implement interoplated strings this way, it may still be worth 
> it to add the small amount of code to the compiler for the 
> extra benefits. But this idea is the first I heard that could 
> work.

One more thing.  If we implement it as a string operator (i.e 
i"...") then we can use just the letter 'i' because it doesn't 
live in the identifier namespace.  If we made it a library, we 
should probably give it a longer name (i.e. interp) so we don't 
take away the identifier `i` from the app.

writeln(i"a is $(a)");

vs

import std.typecons : interp;
writeln(interp#"a is $(a)");

There are a handful of benefits to making it a part of the 
language and the importance of these benefits should be carefully 
considered.  It's the shortest syntax, has normal error messages, 
fast compile time performance, no need for extra imports or 
definitions, does not take away identifiers/symbols from the app 
and just looks nicer. If string interpolation is going to be a 
heavily used feature, showing up in code samples promoting the 
language and all over the standard library then it deserves the 
language support for these benefits.  If it was accepted it would 
show up all over the place in my code and we want to make a good 
impression.



More information about the Digitalmars-d mailing list