tomo/test/optionals.tm

327 lines
6.6 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:
return !Struct
enum Enum(X, Y(y:Int)):
func maybe(should_i:Bool)->Enum?:
if should_i:
return Enum.Y(123)
else:
return !Enum
2024-09-11 09:01:17 -07:00
2024-09-10 22:31:31 -07:00
func maybe_int(should_i:Bool)->Int?:
if should_i:
return 123
else:
return !Int
2024-07-01 09:30:22 -07:00
2024-09-11 20:13:41 -07:00
func maybe_int64(should_i:Bool)->Int64?:
if should_i:
2024-09-15 12:33:47 -07:00
return 123[64]
2024-09-11 20:13:41 -07:00
else:
return !Int64
2024-09-10 22:31:31 -07:00
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
2024-07-01 09:30:22 -07:00
else:
2024-09-10 22:31:31 -07:00
return !Num
2024-07-01 09:30:22 -07:00
2024-09-10 22:31:31 -07:00
func maybe_lambda(should_i:Bool)-> func()?:
if should_i:
return func(): say("hi!")
2024-07-01 09:30:22 -07:00
else:
2024-09-10 22:31:31 -07:00
return !func()
2024-09-11 09:39:56 -07:00
func maybe_c_string(should_i:Bool)->CString?:
if should_i:
return ("hi":as_c_string())?
else:
return !CString
func maybe_channel(should_i:Bool)->|Int|?:
if should_i:
return |:Int|?
else:
return !|Int|
2024-09-11 09:56:16 -07:00
func maybe_thread(should_i:Bool)->Thread?:
if should_i:
return Thread.new(func(): pass)
else:
return !Thread
2024-09-10 22:31:31 -07:00
func main():
>> 5?
= 5? : Int?
>> if no:
!Int
else:
5
= 5? : Int?
>> 5? or -1
= 5 : Int
>> 5? or fail("Non-null is falsey")
= 5 : Int
>> 5? or exit("Non-null is falsey")
2024-09-15 13:42:42 -07:00
= 5 : Int
>> (!Int) or -1
= -1 : Int
2024-09-10 22:31:31 -07:00
do:
!! Ints:
>> yep := maybe_int(yes)
= 123?
>> nope := maybe_int(no)
2024-07-01 09:30:22 -07:00
= !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)
2024-09-15 12:33:47 -07:00
= 123[64]?
2024-09-11 20:13:41 -07:00
>> nope := maybe_int64(no)
= !Int64
>> if yep:
>> yep
2024-09-15 12:33:47 -07:00
= 123[64]
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]?
>> nope := maybe_array(no)
= ![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?
>> nope := maybe_bool(no)
= !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"?
>> nope := maybe_text(no)
= !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?
>> nope := maybe_num(no)
= !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]?
2024-09-10 22:31:31 -07:00
>> nope := maybe_lambda(no)
= !func()
2024-09-11 10:46:38 -07:00
>> if yep:
>> yep
= func() [optionals.tm:54]
2024-09-10 22:31:31 -07:00
else: fail("Falsey: $yep")
>> 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")?
>> nope := Struct.maybe(no)
= !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)
= !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")?
>> nope := maybe_c_string(no)
= !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
do:
!! ...
!! Channels:
>> yep := maybe_channel(yes)
# No "=" test here because channels use addresses in the text version
>> nope := maybe_channel(no)
= !|:Int|
>> if yep: >> yep
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")
else: !! Falsey: $nope
2024-09-11 09:56:16 -07:00
do:
!! ...
!! Threads:
>> yep := maybe_thread(yes)
# No "=" test here because threads use addresses in the text version
>> nope := maybe_thread(no)
= !Thread
>> if yep: >> yep
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 : Int
else: fail("Unreachable")
2024-09-11 21:13:53 -07:00
>> maybe_int(yes)!
= 123 : Int
# Test comparisons, hashing, equality:
>> (!Int == 5?)
= no
>> (5? == 5?)
= yes
>> {!Int, !Int}
= {!Int}
>> [5?, !Int, !Int, 6?]:sorted()
= [!Int, !Int, 5?, 6?]
do:
>> value := if var := 5?:
var
else:
0
= 5
do:
>> value := if var := !Int:
var
else:
0
= 0
do:
>> opt := 5?
>> if opt:
>> opt
else:
>> opt
do:
>> opt := !Int
>> if opt:
>> opt
else:
>> opt
2024-09-16 13:10:05 -07:00
>> not 5?
= no
>> not !Int
= 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")?]