Moving case classes from an abstract class requires compilation. In this case, they are also in the same volume, so there is nothing to import.
Also note that the import error does not apply here, since Main and Expr defined in the same package. that is, the default package.
object Main { def main(args:Array[String]) { var expr1 = new Sum(new Num(1), new Prod(new Num(2), new Num(3))) print(expr1) } } abstract class Expr { } case class Num(n: Int) extends Expr case class Sum(l: Expr , r: Expr) extends Expr case class Prod(l: Expr, r: Expr) extends Expr def evalExpr(e: Expr): Int = e match { case Num(n) => n case Sum(l, r) => evalExpr(l) + evalExpr(r) case Prod(l, r) => evalExpr(l) * evalExpr(r) } def printExpr(e: Expr) : Unit = e match { case Num(n) => print(" " + n + " ") case Sum(l, r) => printExpr(l); print("+"); printExpr(r) case Prod(l, r) => printExpr(l); print("x"); printExpr(r) }
Running this gives:
scala>Main.main(Array[String]()) Sum(Num(1),Prod(Num(2),Num(3)))
source share