【发布时间】:2012-11-13 02:21:07
【问题描述】:
我正在编写一个小型 DSL。从其他角度来看,它可以用作自定义控制结构。
这是一个小例子
case class Reference(var value : Double) {
def apply() = value
def update(v : Double) = value = v
}
implicit def toReference(ref:Reference) = ref.value
trait Store {
import scala.collection.mutable.Buffer
val index : Buffer[Reference] = Buffer()
def ++ (v : Double) : Reference = {
val r = Reference(v)
index += r
r
}
implicit def toSelf(u : Unit) = this
}
class ExampleStore extends Store {
val a = () ++ 1.0
val b = () ++ 2.0
val c = () ++ 0.5
}
val store = new ExampleStore
store.c() = store.a + store.b
我想访问前面没有this 的类运算符,但除了在表达式开头至少指定() 之外找不到其他方法。我需要一个“前缀”表单
将这个例子改写如下
class ExampleStore extends Store {
val a =++ 1.0
val b =++ 2.0
val c =++ 0.5
}
有人能想出一些技巧让 scala 接受这样的约定吗?
【问题讨论】: