Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object ShapeyThings {
- trait ShapeLike[A] {
- def area(shape: A): Int
- }
- }
- object ShapesForTheShapeGod {
- case class Rectangle(x: Int, y: Int)
- object Rectangle {
- import ShapeyThings._
- implicit object RectangleShape extends ShapeLike[Rectangle] {
- def area(shape: Rectangle) = shape.x * shape.y
- }
- }
- case class Square(a: Int)
- object Square {
- import ShapeyThings._
- implicit object SquareShape extends ShapeLike[Square] {
- def area(shape: Square) = shape.a * shape.a
- }
- }
- }
- object WorkingScope {
- import ShapeyThings._
- import ShapesForTheShapeGod.Square
- def doSomething[A : ShapeLike](shape: A) = println(implicitly[ShapeLike[A]].area(shape))
- def run = doSomething(new Square(3))
- }
- WorkingScope.run //9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement