rows
examples/rows.omg
examples/rows.omg31 lines
1{- Row polymorphism.
2
3 omegyu normalize examples/rows.omg
4
5 The fields of a record type form a row. A function can take a record
6 with the fields it needs "and whatever else": `...r` stands for the rest
7 of the row, and `r` is an ordinary (implicit) argument of kind `Row Type`.
8-}
9
10-- Works on every record that has a `name`.
11let greet
12 : forall {r : Row Type} -> { name : Text, ...r } -> Text
13 = \person -> "Hello, ${person.name}!"
14
15-- Changes one field and keeps the others, whatever they are.
16let birthday
17 : forall {r : Row Type} -> { age : Natural, ...r } -> { age : Natural, ...r }
18 = \person -> person with age = person.age + 1
19
20let ada = { name = "Ada", age = 36, languages = [ "English", "French" ] }
21
22let office = { name = "the office", floor = 3 }
23
24in { greetings = [ greet ada, greet office ]
25 , older = birthday ada
26 , -- Records can be extended, restricted and combined.
27 extended = { email = "ada@example.org", ...ada }
28 , anonymous = ada without name
29 , merged = ada // { age = 37 }
30 , projected = ada.{ name, age }
31 }