[BuckleScript 8.1 New Syntax]: String interpolation


#8

Oh, tagged templates would be awesome, especially if you find a way to make them type safe.

AFAICT, PPXes in Reason play the about the same role as tagged templates in JS, but less syntax to learn is probably better.


#9

@IwanKaramazow shall I file an issue regarding unicode in the new BuckleScript syntax repo?


#10

Yes, let’s make a plan of action in the issue!


#11

I’m not sure what you mean by

Just without proper unicode support.

This code seems to do what I want:

let str = j`çóðè`;
Js.log(j`Üñî${str} interpolation works`);

#12

It would be great if multi line strings would support indentation. So if the last backtick is on a new line, the indentation of the last backtick would be used for the multi line string.


#13

@jdeisenberg I was referring to his j-less code snippet. It’d be nice to fuse the two but that’d mean even tighter integration with BuckleScript.

@Jfrolich we’re punting on this for now because there might actually be better multiline string solutions from e.g. Python and Swift IIRC that we can take.


#14

@jfrolich Here is an analysis of choices to be made for multi-line strings: https://openjdk.java.net/jeps/326 (see the section titled “Margin Management.”) Their solution is to provide functions to do the alignment, but the things they had to think about are worth reading.


#15

Sure. My approach is exactly how the python and swift one works.


#16

Having a type safe Unicode safe string interpolation mechanism would really be great!


#17

We made some changes to the current template literal syntax providing unicode support and typesafe string interpolation out of the box. Available in master and will be released soon.

let x = `Sehr schön, ${son.name} 🙌`
// parses as
let x = {js|Sehr schön, |js} ++ son.name ++ {js| 🙌|js}

Other variations are still available through use of a tag:

let sql = sql`select * from myTable`
let x = j`foo $bar`

#18

Hi. What is the use case of other tags? Isn’t it better to leave that syntax for supporting real template literals at some point (functions that receive two arrays - text and interpolation values). That would be amazing for mapping to modern JS libraries.


#19

That’s really great! Thanks a lot!

What about

"Sehr schön"

though? Will it be parsed as a Unicode string, too?


#20

For now only template literals (between backticks) are supported. In the long term we should investigate if this is possible.


#21

Ok - shouldn’t we keep the GitHub issue open then?


#22

This is much better than JS. In JS it’s extremely confusing because sql would be a function and you use it with the literal string. :grimacing: Here it’s a ppx or macro or whatever but the meta programming universe is completely separate from functions!


#23

It would be nice thought to have better interop with the tagged template literals in js.


#24

Let’s keep track of it in a separate issue. Changing “normal” strings as `“sehr schöne string” is going to be more complicated.


#25

Done (https://github.com/BuckleScript/syntax/issues/34).


#26

is there an easy way to update the syntax on a project to try this new String interpolation mechanism or should I better wait for a new release of BuckleScript?


#27

We haven’t released a conversion script yet. Next release will feature it. This is so that we don’t have folks prematurity convert entire projects over before the syntax’s publicly finalized. For example, the recent list and this interpolation discussion changed the new syntax. It’d have been unnecessarily churn to re-convert a project again (not to mention we’d have needed a migration from current new syntax to next release new syntax).