ScalaQuine

· Read in about 1 min · (107 words) ·

A Scala quine ( program that prints itself ):

$ cat ScalaQuine.scala

Here it is:

object ScalaQuine extends App {
  val x = """object ScalaQuine extends App {%c  val x =  %c%c%c%s%c%c%c%c  println(x.format('\n', '\"', '\"', '\"', x, '\"', '\"', '\"', '\n', '\n'))%c}"""
  println(x.format('\n', '\"', '\"', '\"', x, '\"', '\"', '\"', '\n', '\n'))
}

Compile and run:

$ scalac ScalaQuine.scala 
$ scala ScalaQuine

Same as original program:

object ScalaQuine extends App {
  val x =  """object ScalaQuine extends App {%c  val x =  %c%c%c%s%c%c%c%c  println(x.format('\n', '\"', '\"', '\"', x, '\"', '\"', '\"', '\n', '\n'))%c}"""
  println(x.format('\n', '\"', '\"', '\"', x, '\"', '\"', '\"', '\n', '\n'))
}

This is fun!