Created
June 23, 2013 21:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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