Having Java classes:
class abstract Exp
class ExpAdd extends Exp
class ExpSub extends Exp
and Scala methods:
def interpret(e: ExpAdd) = interpret(e.left_arg) + interpret(e.right_arg)
def interpret(e: ExpSub) = interpret(e.left_arg) - interpret(e.right_arg)
def interpret(exp: Exp) = exp match {
case e : ExpAdd = interpret(e)
case e : ExpSub = interpret(e)
}
How to refactor the code to make adding new Exps easier? I thought interpret(Exp) wouldn't be used at all as two other are more specific and
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
however that seems not to be the case in Scala. I also tried removing interpret(Exp) but compiler didn't like it. That's why I used case clause... is there any way to make overloading work as expected? or maybe some workaround?
make, so of course calls tomakeare going to callmake?