Created
January 7, 2019 15:12
-
-
Save vvviiimmm/8a25e99acc61c606b70dada0274c04dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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