tags:

views:

77

answers:

1

I am looking to write a system which allows a syntax like the following:

a b/c

With a being a class, b being a specific term (not any arbitrary string) and c being an Int.

I realize you can do

a b c

which becomes

a.b(c)

However, the first syntax would be more elegant and any solution would help me expand my scala knowledge :)

+2  A: 

If any of these is good enough for you...

a { b/c }
a ( b/c )

...this will do the trick:

trait Term { def / (n:Int):(Term,Int) = (this,n) }

case object Inc extends Term  // any other terms declared likewise

and then your class could support it this way:

class A { 
   def apply(t:(Term,Int)) = t match { 
     case (Inc,n) => n + 1 
     // further term implementations
   } 
}

Usage:

scala> val a = new A
a: A = A@1c2628

scala> a { Inc / 8 }
res28: Int = 9

scala> a ( Inc / 8 )
res29: Int = 9
Germán