Created
December 15, 2021 16:57
-
-
Save j-thepac/94f31e1cf276e5e37d3149e69e0de1c8 to your computer and use it in GitHub Desktop.
Scala Rules
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
// They’re an idiomatic way of representing data using “ands” and “ors”. For example: | |
// a shape is a rectangle or a circle | |
// a rectangle has a width and a height | |
// a circle has a radius | |
// In ADT (Algebraic Data Types (ADTs))terminology, “and” types such as rectangle and circle are products, | |
// whereas the “or” types such as shape are coproducts. | |
// In Scala, we typically represent products using case classes and coproducts using sealed traits: | |
sealed trait Shape | |
final case class Rectangle(width: Double, height: Double) extends Shape | |
final case class Circle(radius: Double) extends Shape |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment