Scala:某些 UDT 上的运算符重载最小 "Type Class"?
Scala: Minimal "Type Class" for operator overloading on some UDT?
我正在尝试学习 scala "Type Class" 功能,但这是我失败的尝试。在此示例中,我尝试在 UDT 上创建可插入运算符:
package playground
// my UDT
class Element(val value : Integer)
trait AdditiveOps {
def +(e2: Element): Element
def -(e2: Element): Element
}
// One possible implementation of AdditiveOps
class AdditiveOpsImpl(val e1: Element) extends AdditiveOps {
def +(e2: Element): Element = {
new Element(e1.value + e2.value)
}
def -(e2: Element): Element = {
new Element(e1.value - e2.value)
}
}
// What needs to be imported
object ElementOps {
implicit def +(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
implicit def -(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
}
然后像这样使用它:
package playground
object Main {
import ElementOps._
def main(args: Array[String]): Unit = {
val e1 = new Element(2)
val e2 = new Element(3)
val e3 = e1 + e2
println("Hello Class Types %s".format(e3.value))
}
}
但我收到以下编译器错误:
type mismatch; found : e1.type (with underlying type playground.Element) required: ?{def +(x: ? >: playground.Element): ?} Note that implicit conversions are not applicable because they are ambiguous: both method + in object ElementOps of type (e1: playground.Element)playground.AdditiveOps and method - in object ElementOps of type (e1: playground.Element)playground.AdditiveOps are possible conversion functions from e1.type to ?{def +(x: ? >: playground.Element): ?} Main.scala /Playground/src/playground line 9 Scala Problem
哦,发现问题了,它在我的 ElementOps
对象实现中。我需要:
object ElementOps {
implicit def additive(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
}
基本上只实现一次隐式方法并以我喜欢的任何方式命名它,例如additive
但它可以是 whatever
并且不使用运算符。修复后,预期的输出是:Hello class types 5
我正在尝试学习 scala "Type Class" 功能,但这是我失败的尝试。在此示例中,我尝试在 UDT 上创建可插入运算符:
package playground
// my UDT
class Element(val value : Integer)
trait AdditiveOps {
def +(e2: Element): Element
def -(e2: Element): Element
}
// One possible implementation of AdditiveOps
class AdditiveOpsImpl(val e1: Element) extends AdditiveOps {
def +(e2: Element): Element = {
new Element(e1.value + e2.value)
}
def -(e2: Element): Element = {
new Element(e1.value - e2.value)
}
}
// What needs to be imported
object ElementOps {
implicit def +(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
implicit def -(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
}
然后像这样使用它:
package playground
object Main {
import ElementOps._
def main(args: Array[String]): Unit = {
val e1 = new Element(2)
val e2 = new Element(3)
val e3 = e1 + e2
println("Hello Class Types %s".format(e3.value))
}
}
但我收到以下编译器错误:
type mismatch; found : e1.type (with underlying type playground.Element) required: ?{def +(x: ? >: playground.Element): ?} Note that implicit conversions are not applicable because they are ambiguous: both method + in object ElementOps of type (e1: playground.Element)playground.AdditiveOps and method - in object ElementOps of type (e1: playground.Element)playground.AdditiveOps are possible conversion functions from e1.type to ?{def +(x: ? >: playground.Element): ?} Main.scala /Playground/src/playground line 9 Scala Problem
哦,发现问题了,它在我的 ElementOps
对象实现中。我需要:
object ElementOps {
implicit def additive(e1: Element) : AdditiveOps = {
new AdditiveOpsImpl(e1)
}
}
基本上只实现一次隐式方法并以我喜欢的任何方式命名它,例如additive
但它可以是 whatever
并且不使用运算符。修复后,预期的输出是:Hello class types 5