Created
January 11, 2022 23:17
-
-
Save meowpunch/3d76f1ab5aa37e11bed21f9be0cdb4f3 to your computer and use it in GitHub Desktop.
improved geometry class with deconstruction patterns
This file contains 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 Point(x: Double, y: Double) {} | |
case class Square(topLeft: Point, side: Double) extends Shape {} | |
case class Rectangle(topLeft: Point, height: Double, width: Double) extends Shape {} | |
case class Circle(center: Point, radius: Double) extends Shape {} | |
class Geometry { | |
val PI = 3.141592653589793 | |
def area(shape: Shape): Double = { | |
shape match { | |
case Square(_, s) => s * s | |
case Rectangle(_, h, w) => h * w | |
case Circle(_, r) => PI * r * r | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment