远程语法糖对Scala中的BigInt不起作用

时间:2014-06-24 09:06:31

标签: scala import syntactic-sugar

我有一个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 + Poverloaded method value * with alternatives (...) cannot be applied to (Point) a * p就可以了。

编译器说{{1}}

如果糖只是粘贴在同一个文件中而不是导入,那么它可以很好地工作。

怎么样?

0 个答案:

没有答案