6

String interpolation works fine in this case:

val name = "Bill"
val result = s"My Name is ${name}"

When I introduce it to the variable it didn't get interpolated value:

val name = "Bill"
val greeting = "My Name is ${name}"
val result = s"${greeting}"

Direct wrapping of greeting is not appropriate solution, I must handle greeting like a plain String.

pacman
  • 797
  • 10
  • 28
  • 1
    Interpolating `greeting` makes no sense, as it's already a string – cchantep Jun 02 '18 at 09:44
  • 1
    @pacman What is the question? – Tim Jun 02 '18 at 11:23
  • Do you want a function `(name: String) => s"My Name is ${name}"`? Or do you want `name` to be bound dynamically to whichever variable called name is in scope at the time you call `s"${greeting}"`? Hint: go with the function either way. It's far less confusing. – Jasper-M Jun 02 '18 at 12:12
  • @Tim My question is: how to evaluate usual string as interpolated? – pacman Jun 03 '18 at 07:04
  • @Jasper-M I had never mentioned any function which you provided in your comment, I want to evaluate usual String as an interpolated. – pacman Jun 03 '18 at 07:06
  • 1
    There's often a difference between what people say they want, what people really want, and what people need. – Jasper-M Jun 03 '18 at 10:24
  • Firstly, you're missing the `s` from before the string in `greeting`, so it's just printing "My Name is ${name}" instead of "My Name is Bill" (not sure if this is just a typo or actually an error in your code). Secondly, why are you not just doing `val result = greeting`? Wrapping it in another string seems redundant. – James Whiteley Jun 04 '18 at 13:13
  • 1
    I think this is a well-formed question. It's obvious what `result` is expected, and it's OK not to care what the type of `greeting` is, just as users don't care what the expression `s"$greeting"` actually does. – som-snytt Jun 04 '18 at 16:38
  • See https://stackoverflow.com/questions/13260864/string-interpolation-in-scala-2-10-how-to-interpolate-a-string-variable, https://stackoverflow.com/questions/36655603/scala-string-interpolator-on-existing-string-with-embedded-variables, https://stackoverflow.com/questions/26888933/calling-a-string-interpolator-on-a-variable-in-scala, probably more. – Alexey Romanov Jul 22 '18 at 12:31
  • did you find solution for this question? – User6006 Apr 16 '20 at 09:56

2 Answers2

6

String interpolation in Scala does not compose in the way you expect.

The issue for this has been debated. Folks want it, but folks don't always get what they want.

You could imagine writing some macros that work in concert. One defines a function taking a string, the other knows how to interpolate it by invoking it with the correctly named value in scope.

Also worth adding that interpolation is not a generic runtime templating mechanism. For example, you can't read strings from a file of the form "$greeting" and run interpolation substitutions on it.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
3
  • There is an unused variable name with value "Bill"
  • There is a String variable greeting which contains characters ['M','y',' ','n','a','m','e',' ','i','s',' ','$','{','n','a','m','e','}'] (completely unrelated to the variable name, the four characters 'n', 'a', 'm', 'e' have no possibility of getting anywhere near compiler's symbol table, which is good).
  • There is a variable result, whose value is computed by string interpolation from greeting. Since

    val y = "whatever"
    val x = s"${y}"
    

    must always result in variable x having the same content as variable y, the variable result ends up having the same value as greeting, namely "My Name is ${name}".

If the compiler tried to execute every substring that looks somewhat like Scala code, this would result in total chaos, because every time something can leak from the realm of String-data into the realm of executable code, it makes the code vulnerable for all kind of injection exploits.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93