-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecords_examples.fold
More file actions
37 lines (26 loc) · 942 Bytes
/
Records_examples.fold
File metadata and controls
37 lines (26 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type Pair a = { first: a,
second: a }
-- A generic function
function swap pair::(Pair a) -> Pair a
{ first, second } = pair
{ first: second, second: first }
end
swap {first, second} = {first: second, second: first}
fn swap<T>(pair: Pair<T>) -> Pair<T> {
let Pair { first, second } = pair;
Pair { first: second, second: first }
}
// Reimplementing a 2-element tuple as a tuple struct
struct Tuple2<T, U>(T, U);
fn main() {
// Explicitly specialize `Pair`
let pair_of_chars: Pair<char> = Pair { first: 'a', second: 'b' };
// Implicitly specialize `Pair`
let pair_of_ints = Pair { first: 1i32, second: 2 };
// Explicitly specialize `Tuple2`
let _tuple: Tuple2<char, i32> = Tuple2('R', 2);
// Explicitly specialize `swap`
let _swapped_pair_of_chars = swap::<char>(pair_of_chars);
// Implicitly specialize `swap`
let _swapped_pair_of_ints = swap(pair_of_ints);
}