Friday Fun with Scala Syntax

Written by Dave Gurnell

It’s been a while since I’ve posted, so I thought I’d steal one of Noel’s ideas and throw up two fun Scala programs for your amusement.

Scala has a pretty flexible syntax (although not as flexible as Racket’s, of course) that makes it popular amongst proponents of DSLs. Not all DSLs have to be serious, however, as I’m about to prove.

First up is Noel’s original concept - proof of his love for DSLs, expressed in purest code:

class A(num: Int) {
  def Scala = "It's s" + ("o" * num) + " much fun!"

object I {
  def <(num: Int) = new A(num)

I <3 Scala
// ==> "It's sooo much fun!"

Second is a simpler work that I call “The Startled Lolcat”:

object O { def o = "What has been seen, cannot be unseen." }

// ==> "What has been seen, cannot be unseen."