Content deleted Content added
Line 41:
=== Algebraic Data Types and Pattern Matching ===
The following program fragment declares an [[algebraic data type]] (ADT) named <code>Shape</code>:
<syntaxhighlight lang="Scala">
enum Shape {
case Circle(Int), // has circle radius
case Square(Int), // has side length
case Rectangle(Int, Int) // has height and width
}
</syntaxhighlight>
The ADT has three constructors: <code>Circle</code>, <code>Square</code>, and <code>Rectangle</code>.
The following program fragment uses [[pattern matching]] to destruct a <code>Shape</code> value:
<syntaxhighlight lang="Scala">
def area(s: Shape): Int = match s {
case Circle(r) => 3 * (r * r)
|