Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. trait Shape[A] {
  2. def area(a: A): Double
  3. }
  4.  
  5. case class Circle(radius: Double)
  6. case class Rectangle(width: Double, length: Double)
  7.  
  8. // Here >
  9. implicit object CircleShape extends Shape[Circle] {
  10. override def area(circle: Circle) : Double = math.Pi * math.pow(circle.radius, 2)
  11. }
  12.  
  13. // And here >
  14. implicit object RectangleShape extends Shape[Rectangle] {
  15. override def area(rectangle: Rectangle): Double = rectangle.width * rectangle.length
  16. }
  17.  
  18. def areaOf[A](shape: A)(implicit shapeImpl: Shape[A]): Double = shapeImpl.area(shape)
  19.  
  20. areaOf(Circle(10))
  21. areaOf(Rectangle(5,5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement