понедельник, 16 сентября 2013 г.

Readable string interpolation in Scala

Multiline strings in Scala have a well-known issue: they produce annoying leading whitespaces.
For example:
object MyApp extends App {
  val html = """
    <html>
      <body>
        <p>Lorem ipsum dolor sit amet...</p>
      </body>
    </html>
  """

  println(html)
}
Running this code produces:

····<html>
······<body>
········<p>Lorem ipsum dolor sit amet...</p>
······</body>
····</html>
··  
As you see, there are:
  • One extra line at the beginning
  • Unnecessary leading spaces before html markup
  • One extra line with two spaces at the end
This small library can solve this problem (inspired by Xtend template expressions). Everything you need is to put nice before your multiline string:
import com.github.orionll.scala.ReadableStringInterpolation._

object MyApp extends App {
  val html = nice"""
    <html>
      <body>
        <p>Lorem ipsum dolor sit amet...</p>
      </body>
    </html>
  """

  println(html)
}
Now this code produces:
<html>
  <body>
    <p>Lorem ipsum dolor sit amet...</p>
  </body>
</html>
nice also plays well with expressions inside strings:
import com.github.orionll.scala.ReadableStringInterpolation._

object MyApp extends App {
  val paragraphs = nice"""
    <p>line1</p>
    <p>line2</p>
  """

  val html = nice"""
    <html>
      <body>
        $paragraphs
      </body>
    </html>
  """

  println(html)
}
The output will be exactly you expect it to be:
<html>
  <body>
    <p>line1</p>
    <p>line2</p>
  </body>
</html>
(Now try to run this code with standard s instead of nice and compare the resulting mess with this)