我有一个SyntacticSugar
类用于在一些文件中的椭圆曲线上导入语法糖。
class SyntacticSugar[Point](curve : Curve[Point])
{
case class EllipticOperand (p : Point)
{
def + (q : => Point) = curve.sum(p,q)
def * (n : => BigInt) = curve.times(p,n)
}
implicit def PointToOperand(p : Point) = EllipticOperand(p)
case class EllipticMultiplier (n : BigInt)
{
def * (p : => Point) = curve.times(p,n)
}
implicit def BigIntToOperand (n : BigInt) = EllipticMultiplier(n)
case class EllipticInt (n : Int)
{
def * (p : => Point) = curve.times(p,BigInt(n))
}
implicit def IntToOperand (n : Int) = EllipticInt(n)
}
我使用如下:
trait Curve[T] {
def sum(p: T, q: T): T
def times(p: T, n: Int): T
}
// dummy implementation from Jasper-M (thanks)
class PointCurve extends Curve[Point] {
override def sum(p: Point, q: Point) = Point(p.x+q.x, p.y+q.y)
override def times(p: Point, n: Int) = Point(p.x*n, p.y*n)
}
...
val sugar = new SyntacticSugar(curve)
import sugar._
问题是如果我想2 * P
P : Point
P * 2
它不起作用,而P + P
和overloaded method value * with alternatives (...) cannot be applied to (Point) a * p
就可以了。
编译器说{{1}}
如果糖只是粘贴在同一个文件中而不是导入,那么它可以很好地工作。
怎么样?