I am working on DSL and I am faced with the problem of using methods as infix operators in the chain. I will just try to explain this with some code. I have a Term trait and case Literal and Variable classes extending it. I want to build a list of term instances using some operators.
case class Expr(val terms: List[Term]) { def +(v: String) = Expr(Literal(v) :: terms) def -->(func: List[String] => List[String]) = terms match { case Literal(v) :: ts => Expr(Variable(v, func) :: ts) case _ => throw new Exception("Can only apply function on literal") } } object foo { def bar(name: String) = Expr(Literal(name) :: Nil) }
I expect this to be equivalent to foo.bar("x").-->(all) , but the interpreter seems to see it as foo.bar("x".-->(all)) .
Rouzbeh delavari
source share