tomo/test/optionals.tm

296 lines
5.9 KiB
Plaintext
Raw Normal View History

2024-09-11 09:01:17 -07:00
struct Struct(x:Int, y:Text):
func maybe(should_i:Bool->Struct?):
2024-09-11 09:01:17 -07:00
if should_i:
return Struct(123, "hello")
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-11 09:01:17 -07:00
enum Enum(X, Y(y:Int)):
func maybe(should_i:Bool->Enum?):
if should_i:
return Enum.Y(123)
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-11 09:01:17 -07:00
func maybe_int(should_i:Bool->Int?):
2024-09-10 22:31:31 -07:00
if should_i:
return 123
else:
2024-12-07 13:04:25 -08:00
return none
2024-07-01 09:30:22 -07:00
func maybe_int64(should_i:Bool->Int64?):
2024-09-11 20:13:41 -07:00
if should_i:
return Int64(123)
2024-09-11 20:13:41 -07:00
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-11 20:13:41 -07:00
func maybe_array(should_i:Bool->[Int]?):
2024-09-10 22:31:31 -07:00
if should_i:
return [10, 20, 30]
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-10 22:31:31 -07:00
func maybe_bool(should_i:Bool->Bool?):
2024-09-10 22:31:31 -07:00
if should_i:
return no
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-10 22:31:31 -07:00
func maybe_text(should_i:Bool->Text?):
2024-09-10 22:31:31 -07:00
if should_i:
return "Hello"
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-10 22:31:31 -07:00
func maybe_num(should_i:Bool->Num?):
2024-09-10 22:31:31 -07:00
if should_i:
return 12.3
2024-07-01 09:30:22 -07:00
else:
2024-12-07 13:04:25 -08:00
return none
2024-07-01 09:30:22 -07:00
func maybe_lambda(should_i:Bool-> func()?):
2024-09-10 22:31:31 -07:00
if should_i:
return func(): say("hi!")
2024-07-01 09:30:22 -07:00
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-10 22:31:31 -07:00
func maybe_c_string(should_i:Bool->CString?):
2024-09-11 09:39:56 -07:00
if should_i:
return ("hi":as_c_string())?
else:
2024-12-07 13:04:25 -08:00
return none
2024-09-11 09:39:56 -07:00
2024-09-10 22:31:31 -07:00
func main():
>> 5?
= 5?
2024-09-10 22:31:31 -07:00
>> if no:
2024-12-07 13:04:25 -08:00
none:Int
2024-09-10 22:31:31 -07:00
else:
5
= 5?
2024-09-10 22:31:31 -07:00
>> 5? or -1
= 5
>> 5? or fail("Non-null is falsey")
= 5
>> 5? or exit("Non-null is falsey")
= 5
2024-09-15 13:42:42 -07:00
2024-12-07 13:04:25 -08:00
>> (none:Int) or -1
= -1
2024-09-10 22:31:31 -07:00
do:
!! Ints:
>> yep := maybe_int(yes)
= 123?
2024-09-10 22:31:31 -07:00
>> nope := maybe_int(no)
= none:Int
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= 123
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-11 20:13:41 -07:00
do:
!! ...
!! Int64s:
>> yep := maybe_int64(yes)
= Int64(123)?
2024-09-11 20:13:41 -07:00
>> nope := maybe_int64(no)
= none:Int64
2024-09-11 20:13:41 -07:00
>> if yep:
>> yep
= Int64(123)
2024-09-11 20:13:41 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-10 22:31:31 -07:00
do:
!! ...
!! Arrays:
>> yep := maybe_array(yes)
= [10, 20, 30]?
2024-09-10 22:31:31 -07:00
>> nope := maybe_array(no)
= none:[Int]
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= [10, 20, 30]
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Bools:
>> yep := maybe_bool(yes)
= no?
2024-09-10 22:31:31 -07:00
>> nope := maybe_bool(no)
= none:Bool
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= no
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Text:
>> yep := maybe_text(yes)
= "Hello"?
2024-09-10 22:31:31 -07:00
>> nope := maybe_text(no)
= none:Text
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= "Hello"
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Nums:
>> yep := maybe_num(yes)
= 12.3?
2024-09-10 22:31:31 -07:00
>> nope := maybe_num(no)
= none:Num
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= 12.3
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
do:
!! ...
!! Lambdas:
# >> yep := maybe_lambda(yes)
# = func() [optionals.tm:54] : func()?
2024-09-10 22:31:31 -07:00
>> nope := maybe_lambda(no)
= none : func()
# >> if yep:
# >> yep
# = func() [optionals.tm:54]
# else: fail("Falsey: $yep")
2024-09-10 22:31:31 -07:00
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-11 09:01:17 -07:00
do:
!! ...
!! Structs:
>> yep := Struct.maybe(yes)
= Struct(x=123, y="hello")?
2024-09-11 09:01:17 -07:00
>> nope := Struct.maybe(no)
= none:Struct
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= Struct(x=123, y="hello")
2024-09-11 09:01:17 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-10 22:48:15 -07:00
do:
!! ...
!! Enums:
>> yep := Enum.maybe(yes)
= Enum.Y(123)?
>> nope := Enum.maybe(no)
= none : Enum
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= Enum.Y(123)
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-11 09:39:56 -07:00
do:
!! ...
!! C Strings:
>> yep := maybe_c_string(yes)
= CString("hi")?
2024-09-11 09:39:56 -07:00
>> nope := maybe_c_string(no)
= none : CString
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= CString("hi")
2024-09-11 09:39:56 -07:00
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-10 22:48:15 -07:00
if yep := maybe_int(yes):
>> yep
= 123
2024-09-10 22:48:15 -07:00
else: fail("Unreachable")
2024-09-11 21:13:53 -07:00
>> maybe_int(yes)!
= 123
# Test comparisons, hashing, equality:
2024-12-07 13:04:25 -08:00
>> (none:Int == 5?)
= no
>> (5? == 5?)
= yes
2024-12-07 13:04:25 -08:00
>> {none:Int, none:Int}
= {none:Int}
2024-12-07 13:04:25 -08:00
>> {:Int? none, none}
= {none:Int}
2024-12-07 13:04:25 -08:00
>> [5?, none:Int, none:Int, 6?]:sorted()
= [none:Int, none:Int, 5, 6]
do:
>> value := if var := 5?:
var
else:
0
= 5
do:
2024-12-07 13:04:25 -08:00
>> value := if var := none:Int:
var
else:
0
= 0
do:
>> opt := 5?
>> if opt:
>> opt
else:
>> opt
do:
2024-12-07 13:04:25 -08:00
>> opt := none:Int
>> if opt:
>> opt
else:
>> opt
2024-09-16 13:10:05 -07:00
>> not 5?
= no
2024-12-07 13:04:25 -08:00
>> not none:Int
2024-09-16 13:10:05 -07:00
= yes
>> [Struct(5,"A")?, Struct(6,"B"), Struct(7,"C")]
= [Struct(x=5, y="A")?, Struct(x=6, y="B")?, Struct(x=7, y="C")?]
if 5? or no:
say("Binary op 'or' works with optionals")
else:
fail("Failed to do binary op 'or' on optional")