Théo Winterhalter
Théo Winterhalter

Reputation: 5108

Having parameter syntactic sugar in scala

I have a bit of syntactic sugar for operations that depends on an object :

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)

I would like to encapsulate in some class SyntacticSugar[Point](curve : main.Curve[Point]) to use it in other classes definitions without having to copy/paste it.

I tried to use it that way :

val sugar = new util.SyntacticSugar(curve)
import sugar._

However, this doesn't work, I can't use + and * after.

Upvotes: 0

Views: 296

Answers (1)

Jasper-M
Jasper-M

Reputation: 15086

If I implement it the way you suggest it just works...

case class Point(x: Int, y: Int)

trait Curve[T] {
  def sum(p: T, q: T): T
  def times(p: T, n: Int): T
}

// dummy implementation
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)
}


object util {
  class SyntacticSugar[T](curve: Curve[T]){
    case class EllipticOperand(p: T){
      def +(q: =>T) = curve.sum(p, q)
      def *(n: =>Int) = curve.times(p,n)
    }
    implicit def point2Operand(p: T) = EllipticOperand(p)
  }
}

Now you can use + and * as operators on a Point:

scala> val sugar = new util.SyntacticSugar(new PointCurve)
sugar: util.SyntacticSugar[Point] = util$SyntacticSugar@4ed4b486

scala> import sugar._
import sugar._

scala> Point(1,2) + Point(2,3)
res0: Point = Point(3,5)

scala> Point(1,2) * 3
res1: Point = Point(3,6)

Upvotes: 4

Related Questions