Skip to content

Instantly share code, notes, and snippets.

@meowpunch
Created January 11, 2022 23:17
Show Gist options
  • Save meowpunch/3d76f1ab5aa37e11bed21f9be0cdb4f3 to your computer and use it in GitHub Desktop.
Save meowpunch/3d76f1ab5aa37e11bed21f9be0cdb4f3 to your computer and use it in GitHub Desktop.
improved geometry class with deconstruction patterns
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