Skip to content

Instantly share code, notes, and snippets.

@theevocater
Created June 23, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theevocater/5846663 to your computer and use it in GitHub Desktop.
Save theevocater/5846663 to your computer and use it in GitHub Desktop.
Trying to get a "scala" version of https://github.com/lkuper/aw/ and have to do this to get it to :load in the REPL. The REPL only recognizes companion objects defined on the same line as the original class so here we have everything defined on the same "line". If I'm wrong someone please tell me how to make this less awkward.
abstract class Expr {
}; case class Number(a: Int) extends Expr {
}; case class Plus(a: Expr, b: Expr) extends Expr {
}; case class Times(a: Expr, b: Expr) extends Expr {
}; case class Minus(a: Expr, b: Expr) extends Expr {
}; object Expr {
def interp(expr: Expr): Int =
expr match {
case Number(a) => a
case Plus(a,b) => interp(a) + interp(b)
case Times(a,b) => interp(a) * interp(b)
case Minus(a,b) => interp(a) - interp(b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment