Given the following:
case class Num(value: Double) {
def plus(rhs: Num) = Num(value + rhs.value)
def times(rhs: Num) = Num(value * rhs.value)
}
object TestApp exten开发者_如何学Gods App {
// ** maybe do something here
val one = Num(1)
val two = Num(2)
val three = Num(3)
val result = three plus one times two
}
Is there a way to rename the plus
method/function in Num
to +plus
and the times
method to *times
at runtime such that result = three +- one *- two
?
It's very important that this happens at runtime. I'd like to avoid compiler plugins if possible. Also, I'm not against java examples, if applicable.
The reason I'd like to do this is because the default operator precedence in Scala for three plus one times two
results in 8 while three +- one *- two
results in 5.
You're assuming operator precedence will work by naming things the way you want, and that's not gonna work. The quickest way I can think of to make this work is with a pimp, as @Dave Griffith suggested:
case class Num(value: Double)
object Main {
implicit def toDouble(v: Num) = v.value
implicit def toNum(v: Double) = Num(v)
def main(args: Array[String]) {
val one = Num(1)
val two = Num(2)
val three = Num(3)
val result: Num = three + one * two
println(result)
}
}
What's more is that you can't do this at "runtime". How would you expect to make the call to a function whose name doesn't exist at compile time? i.e. Num.+plus
where Num
has no +plus
method? The compiler is going to tell you to get lost. And, as was already stated, +plus
isn't valid anyway.
EDIT: I was looking at this again today and I'm not sure what I was smoking. A much better Pimp is:
class NumMath(u: Num) {
def +(v: Num) = Num(u.value + v.value)
def *(v: Num) = Num(u.value * v.value)
}
object Num {
implicit def toNumMath(v: Num) = new NumMath(v)
}
case class Num(value: Double)
object Main {
import Num._
def main(args: Array[String]) {
val one = Num(1)
val two = Num(2)
val three = Num(3)
val result = three + one * two
println(result)
}
}
+plus
and *times
aren't valid Scala names. Names that start with non-alphabetic characters (other than _ and $) must include only non-alphabetic characters.
Other than that, the semantics you want could be done easily with an implicit conversion.
精彩评论