How does Scala "want" me to define s-expr? In English, we define s-expr recursively, like this: "An s-expr is either an atom or a list of s-exprs." How do you say that in Scala?
I'm pretty sure this is wrong:
// Scala 2.11.2
trait Sexpr
case class Atom(text: String) extends Sexpr
type List[Sexpr] = Sexpr // To my amazement, the compiler accepts this!
val blah = Atom("blah")
def matchtest(sexpr: Sexpr): Unit = sexpr match {
case blah :: Nil => println(blah) // But the compiler won't accept this
case _ => println("no match")
}
matchtest(List(Atom("blah")))
Either
is probably not a good fit for this, either, since then I'd have to distinguish between Left
and Right
, which is beside the point.
How do you make a recursive class definition like this so that it works nicely with the rest of Scala?