Question

I wonder why Scala doesn't compile the following code:

val bar = "p1"
val baz = "p2"
val str = s"foo=\"-param1$bar -param2$baz\""

I want variable "str" to be something like

foo="-param1p1 -param2p2"

And the compiler error is

value - is not a member of String
val string = s"foo=\"-param1$bar -param2$baz\""
                     ^
Was it helpful?

Solution

It's limitation of interpolated string literals. You can use the s""" blah """ form to get enclosed double quotes.

To wit:

scala> val bar = "p1"
bar: String = p1

scala> val baz = "p2"
baz: String = p2

scala> val str = s"""foo="-param1$bar -param2$baz\""""
str: String = foo="-param1p1 -param2p2"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top