String interpolation in Scala 2.10 - How to interpolate a String variable?

前端 未结 4 1460
面向向阳花
面向向阳花 2020-12-02 22:11

String interpolation is available in Scala starting Scala 2.10

This is the basic example

 val name = \"World\"            //> name  : String = W         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 22:42

    s is actually a method on StringContext (or something which can be implicitly converted from StringContext). When you write

    whatever"Here is text $identifier and more text"
    

    the compiler desugars it into

    StringContext("Here is text ", " and more text").whatever(identifier)
    

    By default, StringContext gives you s, f, and raw* 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 s method just calls toString (as you'd expect) so you can play games like

    class PrintCounter {
      var i = 0
      override def toString = { val ans = i.toString; i += 1; ans }
    }
    
    val pc = new PrintCounter
    def pr[A](a: A) { println(s"$pc: $a") }
    scala> List("salmon","herring").foreach(pr)
    1: salmon
    2: herring
    

    (0 was already called by the REPL in this example).

    That's about the best you can do.

    *raw is 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.

提交回复
热议问题