Skip to content

Commit 82d987d

Browse files
committed
records
1 parent f906886 commit 82d987d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

flix/src/Records.flix

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// Returns the area of the rectangle `r`.
2+
/// The record `r` must have `x` and `y` labels, and no other labels.
3+
def area2(r: {x = Int32, y = Int32}): Int32 = r.x * r.y
4+
5+
/// Computes the area of various rectangle records.
6+
/// Note that the order of labels is immaterial.
7+
def areas(): List[Int32] =
8+
area2({x = 1, y = 2}) ::
9+
area2({y = 2, x = 3}) :: Nil
10+
11+
/// Returns the area of the polymorphic record `r`.
12+
/// Note that the use of the type variable `a` permits the record `r`
13+
/// to have labels other than `x` and `y`.
14+
def polyArea(r : {x = Int32, y = Int32 | a}): Int32 = r.x * r.y
15+
16+
/// Computes the area of various rectangle records.
17+
/// Note that some records have additional fields.
18+
def polyAreas(): List[Int32] =
19+
polyArea({x = 1, y = 2}) ::
20+
polyArea({x = 2, y = 3, z = 4}) :: Nil
21+
22+
// def main(): Unit \ IO =
23+
// areas() |> println;
24+
// polyAreas() |> println

0 commit comments

Comments
 (0)