Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output """ in the "here docs" of scala?

In scala, "here docs" is begin and end in 3 "

val str = """Hi,everyone"""

But what if the string contains the """? How to output Hi,"""everyone?

like image 490
Freewind Avatar asked Jul 26 '10 17:07

Freewind


1 Answers

Since unicode escaping via \u0022 in multi-line string literals won’t help you, because they would be evaluated as the very same three quotes, your only chance is to concatenate like so:

"""Hi, """+"""""""""+"""everyone"""

The good thing is, that the scala compiler is smart enough to fix this and thus it will make one single string out of it when compiling.

At least, that’s what scala -print says.

object o {
  val s = """Hi, """+"""""""""+"""everyone"""
  val t = "Hi, \"\"\"everyone"
}

and using scala -print

Main$$anon$1$o.this.s = "Hi, """everyone";
Main$$anon$1$o.this.t = "Hi, """everyone";

Note however, that you can’t input it that way. The format which scala -print outputs seems to be for internal usage only.

Still, there might be some easier, more straightforward way of doing this.

like image 120
Debilski Avatar answered Oct 12 '22 02:10

Debilski