-
-
Save ahoy-jon/5899639 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Point extends App { | |
trait Semigroup[T] { | |
def append(p1: T, p2: T): T | |
} | |
case class Point2D(x: Int, y: Int) | |
case class Point3D(x: Int, y: Int, z: Int) | |
object Point2D { | |
implicit def toPoint3D(p:Point2D):Point3D = Point3D(p.x,p.y,0) | |
} | |
implicit object Point2DAdder extends Semigroup[Point2D] { | |
def append(p1: Point2D, p2: Point2D): Point2D = Point2D(p1.x + p2.x, p1.y + p2.y) | |
} | |
implicit object Point3DAdder extends Semigroup[Point3D] { | |
def append(p1: Point3D, p2: Point3D): Point3D = Point3D(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z) | |
} | |
case class addWrapper[T:Semigroup](w:T) { | |
def add(t:T):T = implicitly[Semigroup[T]].append(w, t) | |
} | |
implicit def toAddWrapper[T:Semigroup](t:T) = addWrapper(t) | |
val point1 = Point2D(1, 2) | |
val point2 = Point3D(1, 2, 3) | |
println(point2.add(point1)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mais println(point1.add(point2)) ne marchera pas non ?