1
我期待写一个系统,它允许像语法如下:问题上Scala编译器的语法黑客
a b/c
,A是A类,B是一个特定的期限(不任意字符串)和c作为一个Int。
我知道你可以做
a b c
成为
a.b(c)
然而,第一个语法是更优雅,任何解决方案将帮助我扩大我的斯卡拉知识:)
我期待写一个系统,它允许像语法如下:问题上Scala编译器的语法黑客
a b/c
,A是A类,B是一个特定的期限(不任意字符串)和c作为一个Int。
我知道你可以做
a b c
成为
a.b(c)
然而,第一个语法是更优雅,任何解决方案将帮助我扩大我的斯卡拉知识:)
如果这些对你来说足够好...
a { b/c }
a (b/c)
...这将这样的伎俩:
trait Term { def/(n:Int):(Term,Int) = (this,n) }
case object Inc extends Term // any other terms declared likewise
,然后你的类可以支持这样说:
class A {
def apply(t:(Term,Int)) = t match {
case (Inc,n) => n + 1
// further term implementations
}
}
用法:
scala> val a = new A
a: A = [email protected]
scala> a { Inc/8 }
res28: Int = 9
scala> a (Inc/8)
res29: Int = 9