Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Shape[A] {
- def area(a: A): Double
- }
- case class Circle(radius: Double)
- case class Rectangle(width: Double, length: Double)
- // Here >
- implicit object CircleShape extends Shape[Circle] {
- override def area(circle: Circle) : Double = math.Pi * math.pow(circle.radius, 2)
- }
- // And here >
- implicit object RectangleShape extends Shape[Rectangle] {
- override def area(rectangle: Rectangle): Double = rectangle.width * rectangle.length
- }
- def areaOf[A](shape: A)(implicit shapeImpl: Shape[A]): Double = shapeImpl.area(shape)
- areaOf(Circle(10))
- areaOf(Rectangle(5,5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement