Created
July 1, 2013 10:37
-
-
Save evantill/5899875 to your computer and use it in GitHub Desktop.
scala is better stronger faster
This file contains 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
/** | |
* 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tu peux enlever le : [T <% Point3D](p: T) et remplacer par (p:Point3D)