Search code examples
scalaheredocrawstring

How to bind data in heredoc of scala?


val name = "mike"
val str = """Hi, {name}!"""
println(str)

I want it output the str as Hi, mike!, but failed. How to do this?


Solution

  • Scala does not support string interpolation. There is a compiler plugin that implements it at http://github.com/jrudolph/scala-enhanced-strings.

    Without the plugin you can use concatenation or format strings:

    val str = name formatted "Hi, %s!"
    

    or of course

    val str = "Hi, %s!".format(name)