Syntax to break long single-line strings

The new Rescript syntax is great for type-safe string interpolation but is there a way to break a long string which is intended to be single-line?

Something like JS:

`This is a long string with no\
carriage return`

that is equivalent to:

"This is a long string with no carriage return"

Having the same syntax in Rescript would be nice. Today this creates a syntax error Invalid escape code.

2 Likes

I noticed on Node repl, it would print long lines like this:

`This is a long string with no` +
`carriage return`

Do you think it’s a big difference?

no sure, it’s totally OK, it’s actually even more explicit since there’s no hidden character, but the formatter actually changes it back to a single string, for example:

let foo = `this is an extremely long string and you'll notice `  ++
          `that it actually gets reformatted as one string `     ++
          `which is not very practical`

is reformatted back to:

let foo =
  `this is an extremely long string and you'll notice that it actually gets reformatted as one string which is not very practical`

I think the formatter should actually do the other way around.

although it could join the split string when producing the AST, then the splitting would be absolutely 0-cost during runtime.

@tsnobip we should follow the js semantics in this case.
I submitted a PR in https://github.com/rescript-lang/syntax/pull/118

wow that was fast @Maxim, thanks a lot ^^

Implemented and merged.

If you want to use line breaks to make your source code easier to read, but you don’t want the line breaks to be part of the string’s value, write a backslash () at the end of those line.

`This is a long string with no\
 line break`

// interpreted as
 "This is a long string with no line break"
4 Likes