diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-11 01:31:31 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-11 01:31:31 -0400 |
| commit | 7126755275f12e6278031e78ff33f65801b133dd (patch) | |
| tree | 7f43f3449eb7bb69b0879dd41eb174e89fdc34cc /test | |
| parent | 89234e34e292861fccb8e5bdbefc695a7e443eea (diff) | |
Add optional types
Diffstat (limited to 'test')
| -rw-r--r-- | test/arrays.tm | 8 | ||||
| -rw-r--r-- | test/optionals.tm | 128 | ||||
| -rw-r--r-- | test/tables.tm | 2 |
3 files changed, 123 insertions, 15 deletions
diff --git a/test/arrays.tm b/test/arrays.tm index 16f411a2..5856e0a4 100644 --- a/test/arrays.tm +++ b/test/arrays.tm @@ -168,8 +168,8 @@ func main(): >> [10, 20, 30]:find(999) = 0 - >> [10, 20]:first(func(i:&Int): i:is_prime()) - = !Int - >> [4, 5, 6]:first(func(i:&Int): i:is_prime()) - = @%5? + # >> [10, 20]:first(func(i:&Int): i:is_prime()) + # = !Int + # >> [4, 5, 6]:first(func(i:&Int): i:is_prime()) + # = @%5? diff --git a/test/optionals.tm b/test/optionals.tm index 5f6fb78a..d8cbf741 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -1,15 +1,123 @@ +func maybe_int(should_i:Bool)->Int?: + if should_i: + return 123 + else: + return !Int -func main(): - >> opt := @5? - when opt is @nonnull: - >> nonnull[] - = 5 +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: - fail("Oops") + return !Num - >> opt = !@Int - when opt is @nonnull: - fail("Oops") +func maybe_lambda(should_i:Bool)-> func()?: + if should_i: + return func(): say("hi!") else: - >> opt + 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 + diff --git a/test/tables.tm b/test/tables.tm index 7b3595b6..e59283ed 100644 --- a/test/tables.tm +++ b/test/tables.tm @@ -38,7 +38,7 @@ func main(): >> t2.length = 1 >> t2.fallback - = @%{"one":1, "two":2}? + = {"one":1, "two":2}? t2_str := "" for k,v in t2: |
