syntax icon indicating copy to clipboard operation
syntax copied to clipboard

Implicit string interpolation only works for identifier but not expression

Open Shuenhoy opened this issue 3 years ago • 2 comments

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?

Shuenhoy avatar Jan 15 '22 04:01 Shuenhoy

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.

ryyppy avatar Mar 23 '22 07:03 ryyppy

There was a short discussion in https://github.com/rescript-lang/rescript-compiler/issues/2593 about this.

anmonteiro avatar Mar 24 '22 05:03 anmonteiro

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.

chriswilty avatar Dec 14 '22 13:12 chriswilty

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.

stale[bot] avatar May 28 '23 15:05 stale[bot]

This ticket is obsolete, since j / js qualifiers for strings are deprecated in favour of just using backtick strings with ${} syntax.

ryyppy avatar Jun 13 '23 05:06 ryyppy