Reputation: 5977
I'm writing a small DSL. It may be used as a custom control structure from other point of view.
Here comes little example
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
I'd like to access class operators with no preceding this
but could not find other way than specifying at least ()
at start of expressions. A "prefix" form is what I need
Some way to rewrite this example as following
class ExampleStore extends Store {
val a =++ 1.0
val b =++ 2.0
val c =++ 0.5
}
Could anyone think of some trick to make scala accept such convention?
Upvotes: 1
Views: 87
Reputation: 40461
Writing ++ 1.0
means that ++
is an unary operator of 1.0
(class Double). However, in Scala unary operators are limited to ! ~ - +
. So theres is no way to implement the syntax you whish.
But you can still use parenthesis:
class ExampleStore extends Store {
val a = ++(1.0)
val b = ++(2.0)
val c = ++(3.0)
}
Upvotes: 1