Skip to content

Instantly share code, notes, and snippets.

@evantill
Created July 1, 2013 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantill/5899875 to your computer and use it in GitHub Desktop.
Save evantill/5899875 to your computer and use it in GitHub Desktop.
scala is better stronger faster
/**
* Created with IntelliJ IDEA.
* User: evantill
* Date: 01/07/13
* Time: 12:22
* To change this template use File | Settings | File Templates.
*/
object PointBarre extends App {
case class Point2D(x: Int, y: Int) {
def add(p: Point2D) = Point2D(x + p.x, y + p.y)
}
object Point2D {
implicit def toPoint3D(p: Point2D) = Point3D(p.x, p.y, 0)
}
case class Point3D(x: Int, y: Int, z: Int) {
def add[T <% Point3D](p: T) = Point3D(x + p.x, y + p.y, z + p.z)
}
val p1 = Point2D(1, 2)
val p2 = Point3D(1, 2, 3)
Console.println(p1 add p1)
Console.println(p1 add p2)
Console.println(p2 add p1)
Console.println(p2 add p2)
}
@ahoy-jon
Copy link

ahoy-jon commented Jul 1, 2013

Tu peux enlever le : [T <% Point3D](p: T) et remplacer par (p:Point3D)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment