blob: a721deaad8fbca6f9ad901af394f27bdc0c95705 (
plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
struct Struct(x:Int, y:Text):
func maybe(should_i:Bool)->Struct?:
if should_i:
return Struct(123, "hello")
else:
return !Struct
enum Enum(X, Y(y:Int)):
func maybe(should_i:Bool)->Enum?:
if should_i:
return Enum.Y(123)
else:
return !Enum
func maybe_int(should_i:Bool)->Int?:
if should_i:
return 123
else:
return !Int
func maybe_array(should_i:Bool)->[Int]?:
if should_i:
return [10, 20, 30]
else:
return ![Int]
func maybe_bool(should_i:Bool)->Bool?:
if should_i:
return no
else:
return !Bool
func maybe_text(should_i:Bool)->Text?:
if should_i:
return "Hello"
else:
return !Text
func maybe_num(should_i:Bool)->Num?:
if should_i:
return 12.3
else:
return !Num
func maybe_lambda(should_i:Bool)-> func()?:
if should_i:
return func(): say("hi!")
else:
return !func()
func main():
>> 5?
= 5? : Int?
>> if no:
!Int
else:
5
= 5? : Int?
do:
!! Ints:
>> yep := maybe_int(yes)
= 123?
>> nope := maybe_int(no)
= !Int
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Arrays:
>> yep := maybe_array(yes)
= [10, 20, 30]?
>> nope := maybe_array(no)
= ![Int]
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Bools:
>> yep := maybe_bool(yes)
= no?
>> nope := maybe_bool(no)
= !Bool
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Text:
>> yep := maybe_text(yes)
= "Hello"?
>> nope := maybe_text(no)
= !Text
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Nums:
>> yep := maybe_num(yes)
= 12.3?
>> nope := maybe_num(no)
= !Num
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Lambdas:
>> yep := maybe_lambda(yes)
= func(): ...?
>> nope := maybe_lambda(no)
= !func()
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Structs:
>> yep := Struct.maybe(yes)
= Struct(x=123, y="hello")?
>> nope := Struct.maybe(no)
= !Struct
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Enums:
>> yep := Enum.maybe(yes)
= Enum.Y(y=123)?
>> nope := Enum.maybe(no)
= !Enum
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
if yep := maybe_int(yes):
>> yep
= 123 : Int
else: fail("Unreachable")
|