Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Created January 7, 2019 15:12
Show Gist options
  • Save vvviiimmm/8a25e99acc61c606b70dada0274c04dc to your computer and use it in GitHub Desktop.
Save vvviiimmm/8a25e99acc61c606b70dada0274c04dc to your computer and use it in GitHub Desktop.
sealed trait Shape
case class Circle(radius: Int) extends Shape
case class Rectangle(width: Int, height: Int) extends Shape
// 'match' keyword allows us to pattern match on a
// specific option of the sum type
def area(shape: Shape): Double = shape match {
case Circle(r) => math.Pi * r * r
case Rectangle(w, h) => w * h // Note how rectangle's width and height
// are captured in 'w' and 'h'. It's possible
// because Rectange is a product type
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment