[BuckleScript 8.1 New Syntax]: String interpolation


#4

To clarify the multiline string syntax a bit:

let name = `Admin name is ${admin.firstName}  ${admin.lastName}`

is the same as

let name = `Admin name is ` ++ admin.firstName ++  ` ` ++ admin.lastName

So it is just backquotes and we allow arbitrary expression inside ${}.


#5

Great! This is perfect, thanks a lot! :slight_smile:

Then there just remains the issue with Unicode that Cheng mentioned.

Js.log("Sehr schön")
Js.log(`Sehr schön`)
Js.log(j`Sehr schön`)

gives me

Sehr schön
Sehr schön
Sehr schön

Would it be possible to get Unicode to work for all three? This is one of those things that’s very hard to explain to newbies coming from JS…


#6

I like the fact that the new multiline string syntax looks exactly like tagged string literals in JS.

// .js

const style = styled`color: red`;

// .res
let style = styled`color: red`;

I’d also prefer multiline strings to be unicode compatible though


#7

Yes, I think we should work together with bobzhang to make it unicode compatible and drop the j prefix

I also envisioned the following usecase:

sql`select * from myTable`

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