Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Last active June 3, 2020 07:49
Show Gist options
  • Select an option

  • Save vvviiimmm/bed7beadd194f0921113987a37e6789e to your computer and use it in GitHub Desktop.

Select an option

Save vvviiimmm/bed7beadd194f0921113987a37e6789e to your computer and use it in GitHub Desktop.
trait Shape {
def area: Double
}
// Shape definition data structures, should be in a diffrent file/namespace
case class Circle(radius: Double)
case class Rectangle(width: Double, length: Double)
// Implementation 1
class CircleShape(radius: Double) extends Shape {
override def area = math.Pi * math.pow(radius, 2)
}
// Implementation 2
class RectangleShape(width: Double, length: Double) extends Shape {
override def area: Double = width * length
}
def areaOf(shape: Shape): Double = shape.area
areaOf(new CircleShape(10))
areaOf(new RectangleShape(5, 5))
@nethibernate
Copy link

Should the class CircleShape(radius: Double) and RectangleShape(width: Double, length: Double) be CircleShape(circle: Circle) and RectangleShape(rectangle: Rectangle)?

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