Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Last active February 20, 2017 05:18
Show Gist options
  • Select an option

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

Select an option

Save vvviiimmm/ec8ff86d82a7d22a4eececbe27007333 to your computer and use it in GitHub Desktop.
// Our generic inteface
trait Shape {
def area: Double
}
// Implementation 1
class Circle(radius: Double) extends Shape {
override def area: Double = math.Pi * math.pow(radius, 2)
}
// Implementation 2
class Rectangle(width: Double, length: Double) extends Shape {
override def area: Double = width * length
}
// Generic function
def areaOf(shape: Shape): Double = shape.area
// Usage
areaOf(new Circle(10))
areaOf(new Rectangle(5, 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment