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.

10
  • 1
    Interpolating greeting makes no sense, as it's already a string Commented Jun 2, 2018 at 9:44
  • 1
    @pacman What is the question? Commented Jun 2, 2018 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. Commented Jun 2, 2018 at 12:12
  • 1
    There's often a difference between what people say they want, what people really want, and what people need. Commented Jun 3, 2018 at 10:24
  • 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. Commented Jun 4, 2018 at 16:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.