syntax
syntax copied to clipboard
Implicit string interpolation only works for identifier but not expression
let a = 1
let b =j`$a, $(a), ${a+1}`
Take this for example. The first and second interpolation work, however, the third ${a+1} would trigger an error:
This has type: int
Somewhere wanted: string
You can convert int to string with Belt.Int.toString.
Is this by design?
Wow this is the first time I am seeing anyone mixing these two features.
Generally speaking: The j interpolation only auto-coerces variables that are prefixed with an $ ($a). The interpolation expressions (${...}) always require to explicitly convert to a string.
These are two independent features btw. You'd usually not use the old j interpolation version and just use the new interpolation expression syntax instead.
There was a short discussion in https://github.com/rescript-lang/rescript-compiler/issues/2593 about this.
I think the docs should be clearer on this then:
These are two independent features btw. You'd usually not use the old j interpolation version and just use the new interpolation expression syntax instead.
If the j syntax is old, does that mean it is deprecated? The benefit of the j syntax is that you don't need to convert values into string, which seems unnecessarily verbose.
Also, I only discovered by reading this issue that brackets can be used to separate an interpolated variable from a raw string, e.g. $(mySpeed)m/s. While the j syntax is still supported, it would be good to add an example like this.
The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.
This ticket is obsolete, since j / js qualifiers for strings are deprecated in favour of just using backtick strings with ${} syntax.