code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(280 lines)
2 struct Struct(x:Int, y:Text)
3 func maybe(should_i:Bool->Struct?)
4 if should_i
5 return Struct(123, "hello")
6 else
7 return none
9 enum Enum(X, Y(y:Int))
10 func maybe(should_i:Bool->Enum?)
11 if should_i
12 return Enum.Y(123)
13 else
14 return none
16 func maybe_int(should_i:Bool->Int?)
17 if should_i
18 return 123
19 else
20 return none
22 func maybe_int64(should_i:Bool->Int64?)
23 if should_i
24 return Int64(123)
25 else
26 return none
28 func maybe_list(should_i:Bool->[Int]?)
29 if should_i
30 return [10, 20, 30]
31 else
32 return none
34 func maybe_bool(should_i:Bool->Bool?)
35 if should_i
36 return no
37 else
38 return none
40 func maybe_text(should_i:Bool->Text?)
41 if should_i
42 return "Hello"
43 else
44 return none
46 func maybe_num(should_i:Bool->Num?)
47 if should_i
48 return 12.3
49 else
50 return none
52 func maybe_lambda(should_i:Bool-> func()?)
53 if should_i
54 return func() say("hi!")
55 else
56 return none
58 func maybe_c_string(should_i:Bool->CString?)
59 if should_i
60 return "hi".as_c_string()
61 else
62 return none
64 func maybe_path(should_i:Bool->Path?)
65 if should_i
66 return ./foo
67 else
68 return none
70 func main()
71 optional : Int? = 5
72 assert optional == 5
74 assert (
75 if no
76 x : Int? = none
78 else
80 ) == 5
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
91 do
92 say("Ints:")
93 yep := maybe_int(yes)
94 assert yep == 123
95 nope := maybe_int(no)
96 assert nope == none
97 >> if yep
98 assert yep == 123
99 else fail("Falsey: $yep")
100 >> if nope
101 fail("Truthy: $nope")
102 else say("Falsey: $nope")
105 say("Int64s:")
106 yep := maybe_int64(yes)
107 assert yep == Int64(123)
108 nope := maybe_int64(no)
109 assert nope == none
110 >> if yep
111 assert yep == Int64(123)
112 else fail("Falsey: $yep")
113 >> if nope
114 fail("Truthy: $nope")
115 else say("Falsey: $nope")
118 say("Lists:")
119 yep := maybe_list(yes)
120 assert yep == [10, 20, 30]
121 nope := maybe_list(no)
122 assert nope == none
123 >> if yep
124 assert yep == [10, 20, 30]
125 else fail("Falsey: $yep")
126 >> if nope
127 fail("Truthy: $nope")
128 else say("Falsey: $nope")
131 say("...")
132 say("Bools:")
133 yep := maybe_bool(yes)
134 assert yep == no
135 nope := maybe_bool(no)
136 assert nope == none
137 >> if yep
138 assert yep == no
139 else fail("Falsey: $yep")
140 >> if nope
141 fail("Truthy: $nope")
142 else say("Falsey: $nope")
145 say("...")
146 say("Text:")
147 yep := maybe_text(yes)
148 assert yep == "Hello"
149 nope := maybe_text(no)
150 assert nope == none
151 >> if yep
152 assert yep == "Hello"
153 else fail("Falsey: $yep")
154 >> if nope
155 fail("Truthy: $nope")
156 else say("Falsey: $nope")
159 say("...")
160 say("Nums:")
161 yep := maybe_num(yes)
162 assert yep == 12.3
163 nope := maybe_num(no)
164 assert nope == none
165 >> if yep
166 assert yep == 12.3
167 else fail("Falsey: $yep")
168 >> if nope
169 fail("Truthy: $nope")
170 else say("Falsey: $nope")
173 say("...")
174 say("Lambdas:")
175 nope := maybe_lambda(no)
176 assert nope == none
177 >> if nope
178 fail("Truthy: $nope")
179 else say("Falsey: $nope")
182 say("...")
183 say("Structs:")
184 yep := Struct.maybe(yes)
185 assert yep == Struct(x=123, y="hello")
186 nope := Struct.maybe(no)
187 assert nope == none
188 >> if yep
189 assert yep == Struct(x=123, y="hello")
190 else fail("Falsey: $yep")
191 >> if nope
192 fail("Truthy: $nope")
193 else say("Falsey: $nope")
196 say("...")
197 say("Enums:")
198 yep := Enum.maybe(yes)
199 assert yep == Enum.Y(123)
200 nope := Enum.maybe(no)
201 assert nope == none
202 >> if yep
203 assert yep == Enum.Y(123)
204 else fail("Falsey: $yep")
205 >> if nope
206 fail("Truthy: $nope")
207 else say("Falsey: $nope")
210 say("...")
211 say("C Strings:")
212 yep := maybe_c_string(yes)
213 assert yep == CString("hi")
214 nope := maybe_c_string(no)
215 assert nope == none
216 >> if yep
217 assert yep == CString("hi")
218 else fail("Falsey: $yep")
219 >> if nope
220 fail("Truthy: $nope")
221 else say("Falsey: $nope")
224 say("...")
225 say("Paths:")
226 yep := maybe_path(yes)
227 assert yep == ./foo
228 nope := maybe_path(no)
229 assert nope == none
230 >> if yep
231 assert yep == ./foo
232 else fail("Falsey: $yep")
233 >> if nope
234 fail("Truthy: $nope")
235 else say("Falsey: $nope")
237 if yep := maybe_int(yes)
238 assert yep == 123
239 else fail("Unreachable")
241 assert maybe_int(yes)! == 123
243 # Test comparisons, hashing, equality:
244 assert none != optional
245 assert optional == 5
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
257 >> opt : Int? = 5
258 >> if opt
259 >> opt
260 else
261 >> opt
264 >> opt : Int? = none
265 >> if opt
266 >> opt
267 else
268 >> opt
270 assert not optional == no
272 >> nah : Int? = none
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")]
277 if optional or no
278 say("Binary op 'or' works with optionals")
279 else
280 fail("Failed to do binary op 'or' on optional")