2 struct Struct(x:Int, y:Text)
3 func maybe(should_i:Bool->Struct?)
5 return Struct(123, "hello")
10 func maybe(should_i:Bool->Enum?)
16 func maybe_int(should_i:Bool->Int?)
22 func maybe_int64(should_i:Bool->Int64?)
28 func maybe_list(should_i:Bool->[Int]?)
34 func maybe_bool(should_i:Bool->Bool?)
40 func maybe_text(should_i:Bool->Text?)
46 func maybe_num(should_i:Bool->Num?)
52 func maybe_lambda(should_i:Bool-> func()?)
54 return func() say("hi!")
58 func maybe_c_string(should_i:Bool->CString?)
60 return "hi".as_c_string()
64 func maybe_path(should_i:Bool->Path?)
82 assert (optional or -1) == 5
84 assert (optional or fail("Non-none is falsey")) == 5
86 assert (optional or exit("Non-none is falsey")) == 5
88 >> none_int : Int? = none
89 assert none_int or -1 == -1
99 else fail("Falsey: $yep")
101 fail("Truthy: $nope")
102 else say("Falsey: $nope")
106 yep := maybe_int64(yes)
107 assert yep == Int64(123)
108 nope := maybe_int64(no)
111 assert yep == Int64(123)
112 else fail("Falsey: $yep")
114 fail("Truthy: $nope")
115 else say("Falsey: $nope")
119 yep := maybe_list(yes)
120 assert yep == [10, 20, 30]
121 nope := maybe_list(no)
124 assert yep == [10, 20, 30]
125 else fail("Falsey: $yep")
127 fail("Truthy: $nope")
128 else say("Falsey: $nope")
133 yep := maybe_bool(yes)
135 nope := maybe_bool(no)
139 else fail("Falsey: $yep")
141 fail("Truthy: $nope")
142 else say("Falsey: $nope")
147 yep := maybe_text(yes)
148 assert yep == "Hello"
149 nope := maybe_text(no)
152 assert yep == "Hello"
153 else fail("Falsey: $yep")
155 fail("Truthy: $nope")
156 else say("Falsey: $nope")
161 yep := maybe_num(yes)
163 nope := maybe_num(no)
167 else fail("Falsey: $yep")
169 fail("Truthy: $nope")
170 else say("Falsey: $nope")
175 nope := maybe_lambda(no)
178 fail("Truthy: $nope")
179 else say("Falsey: $nope")
184 yep := Struct.maybe(yes)
185 assert yep == Struct(x=123, y="hello")
186 nope := Struct.maybe(no)
189 assert yep == Struct(x=123, y="hello")
190 else fail("Falsey: $yep")
192 fail("Truthy: $nope")
193 else say("Falsey: $nope")
198 yep := Enum.maybe(yes)
199 assert yep == Enum.Y(123)
200 nope := Enum.maybe(no)
203 assert yep == Enum.Y(123)
204 else fail("Falsey: $yep")
206 fail("Truthy: $nope")
207 else say("Falsey: $nope")
212 yep := maybe_c_string(yes)
213 assert yep == CString("hi")
214 nope := maybe_c_string(no)
217 assert yep == CString("hi")
218 else fail("Falsey: $yep")
220 fail("Truthy: $nope")
221 else say("Falsey: $nope")
226 yep := maybe_path(yes)
228 nope := maybe_path(no)
232 else fail("Falsey: $yep")
234 fail("Truthy: $nope")
235 else say("Falsey: $nope")
237 if yep := maybe_int(yes)
239 else fail("Unreachable")
241 assert maybe_int(yes)! == 123
243 # Test comparisons, hashing, equality:
244 assert none != optional
246 >> nones : {Int?:Bool} = {none: yes, none: yes}
247 assert nones.keys == [none]
248 assert [5, none, none, 6].sorted() == [none, none, 5, 6]
251 assert (if var := optional then var else 0) == 5
254 assert (if var : Int? = none then var else 0) == 0
270 assert not optional == no
273 assert not nah == yes
275 assert [none, Struct(5,"A"), Struct(6,"B"), Struct(7,"C")] == [none, Struct(x=5, y="A"), Struct(x=6, y="B"), Struct(x=7, y="C")]
278 say("Binary op 'or' works with optionals")
280 fail("Failed to do binary op 'or' on optional")