【发布时间】:2014-08-10 23:08:56
【问题描述】:
对于依赖于对象的操作,我有一点语法糖:
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)
我想封装一些class SyntacticSugar[Point](curve : main.Curve[Point]) 以便在其他类定义中使用它,而不必复制/粘贴它。
我尝试过这样使用它:
val sugar = new util.SyntacticSugar(curve)
import sugar._
但是,这不起作用,我不能在之后使用+ 和*。
【问题讨论】:
-
为什么不把你混入其他职业的特质呢?
-
问题是,
traits 不支持参数,而且我的语法糖每次都依赖于特定的曲线。顺便谢谢。 -
您这样定义了
SyntacticSugar类:class SyntacticSugar[Point](curve : main.Curve[Point])?那么上面de code sn-p中的Point不是类而是类型参数? -
嗯,
Point确实是一个类型参数,但curve是一个对象。
标签: scala parameters syntactic-sugar