String interpolation is available in Scala starting Scala 2.10
This is the basic example
val name = "World" //> name : String = World
val message = s"Hello $name" //> message : String = Hello World
I was wondering if there is a way to do dynamic interpolation, e.g. the following (doesn’t compile, just for illustration purposes)
val name = "World" //> name : String = World
val template = "Hello $name" //> template : String = Hello $name
//just for illustration:
val message = s(template) //> doesn't compile (not found: value s)
-
Is there a way to “dynamically” evaluate a String like that? (or is it inherently wrong / not possible)
-
And what is
sexactly?it’s not a method def(apparently it is a method onStringContext), and not an object (if it was, it would have thrown a different compile error than not found I think)
sis actually a method onStringContext(or something which can be implicitly converted fromStringContext). When you writethe compiler desugars it into
By default,
StringContextgives yous,f, andraw* methods.As you can see, the compiler itself picks out the name and gives it to the method. Since this happens at compile time, you can’t sensibly do it dynamically–the compiler doesn’t have information about variable names at runtime.
You can use vars, however, so you can swap in values that you want. And the default
smethod just callstoString(as you’d expect) so you can play games like(0 was already called by the REPL in this example).
That’s about the best you can do.
*
rawis broken and isn’t slated to be fixed until 2.10.1; only text before a variable is actually raw (no escape processing). So hold off on using that one until 2.10.1 is out, or look at the source code and define your own. By default, there is no escape processing, so defining your own is pretty easy.