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

Sebastiaan Koppe mail at skoppe.eu
Thu Dec 12 22:34:11 UTC 2019


On Thursday, 12 December 2019 at 17:01:01 UTC, Adam D. Ruppe 
wrote:
> 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

That is a good observation. I forgot all about tagged template 
literals. We should totally steal some of that.

 From the page:

> Template literals are enclosed by the back-tick (` `)  (grave 
> accent) character instead of double or single quotes. Template 
> literals can contain placeholders. These are indicated by the 
> dollar sign and curly braces (${expression}). The expressions 
> in the placeholders and the text between the back-ticks (` `) 
> get passed to a function. The default function just 
> concatenates the parts into a single string. If there is an 
> expression preceding the template literal (tag here), this is 
> called a "tagged template". In that case, the tag expression 
> (usually a function) gets called with the template literal, 
> which you can then manipulate before outputting.

try this in your browser's console:

val = 4;
function sql(strings, ...keys) { return {sql: strings.join("?"), 
keys} };
sql`select * from table where a = ${val} and b = ${val}`;

it will return: {"sql":"select * from table where a = ? and b = 
?","keys":[4,4]}

> 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.

yes.



More information about the Digitalmars-d mailing list