Scala - omit parentheses from apply method for a DSL -
i'm trying create dsl , running problem. have these definitions:
case class var(name: string) case class lam(v: var, t: var) val (a, b) = (var("a"), var("b"))
i want able this:
scala> \ b lam(var(a),var(b))
reading on rules of parenthesis dropping, see need chain functions take 1 parameter each, i've created series of "builder" classes perform construction:
class lambuilderb(v: var) { def apply(t: var) = lam(v, t) } class lambuildera { def apply(v: var) = new lambuilderb(v) } val \ = new lambuildera
i had hoped work since each apply
takes 1 argument. but, doesn't seem dropping parentheses legal apply
since wants treat argument method name:
scala> \(a)(b) res95: lam = lam(var(a),var(b)) scala> \ b error: value not member of lambuildera \ b ^
any ideas how how can dsl syntax without parentheses?
bonus question: can this?:
scala> \a.b lam(var(a),var(b))
you can pretty close using 1 of 4 unary prefix operators (~
, !
, +
, -
):
trait expr { def &(other: expr) = lam(this, other) def unary_~ = } case class var(name: string) extends expr case class lam(a: expr, b: expr) extends expr scala> ~ var("a") & var("b") res0: lam = lam(var(a),var(b)) scala> ~ var("a") & var("b") & var("c") res1: lam = lam(lam(var(a),var(b)),var(c))
Comments
Post a Comment