Flix (programming language): Difference between revisions

Content deleted Content added
JorKadeen (talk | contribs)
JorKadeen (talk | contribs)
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">
/// An algebraic data type for shapes.
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">
/// Computes the area of the given shape using pattern matching and basic arithmetic.
def area(s: Shape): Int = match s {
case Circle(r) => 3 * (r * r)