-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_five_test.gleam
116 lines (88 loc) · 2.12 KB
/
chapter_five_test.gleam
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import dmmf_gleam
import fmt
import gleam/should
// === Modeling Simple Values ===
type CustomerId {
CustomerId(Int)
};
type WidgetCode {
WidgetCode(String)
};
type UnitQuantity {
UnitQuantity(Int)
}
type KilogramQuantity {
KilogramQuantity(Float)
}
// === Working with Single Case Unions ===
type OrderId {
OrderId(Int)
}
pub fn ex_1_test() {
let customer_id = CustomerId(42)
let order_id = OrderId(42)
// We can't confuse different types by accident, we get a
// compiler error:
//
// case customer_id == order_id {
// ^^^^^^^^
// Expected type:
//
// CustomerId
//
// Found type:
//
// OrderId
let CustomerId(inner_value) = customer_id
inner_value
|> should.equal(42)
}
pub fn ex_2_test() {
// construct
let customer_id = CustomerId(42)
// deconstruct
let CustomerId(inner_value) = customer_id
inner_value
|> should.equal(42)
}
// fn process_customer_id(CustomerId(inner_value)) {
// "inner_value is " |> ftm.append(inner_value)
// }
//
// -> Syntax error (Gleam can't deconstruct directly in the
// parameter of a function definition
// === Avoiding Performance Issues with Simple Types
type UnitQuantity3 = Int;
// No translation for [<Struct>]
// No translation for UnitQuantities of int[]
// === Modeling Complex Data / Modeling Unknown Types ===
type Undefined = Nil // Gleam has no exn type
type CustomerInfo = Undefined;
type ShippingAddress = Undefined;
type BillingAddress = Undefined;
type OrderLine = Undefined;
type BillingAmount = Undefined;
type Order {
Order(
customer_info: CustomerInfo,
shipping_address: ShippingAddress,
billing_address: BillingAddress,
order_lines: List(OrderLine),
amount_to_bill: BillingAmount
)
};
// === Modeling with Choice Types ===
// Dummy type
type GizmoCode = Undefined;
type ProductCode {
Widget(WidgetCode)
Gizmo(GizmoCode)
};
type OrderQuantity {
Unit(UnitQuantity)
Kilogram(KilogramQuantity)
}
// === Modeling Workflows with Functions ===
type UnvalidatedOrder = Undefined
type ValidatedOrder = Undefined
type ValidateOrder = fn(UnvalidatedOrder) -> ValidatedOrder