tomo/test/enums.tm

87 lines
1.4 KiB
Plaintext
Raw Normal View History

2024-05-01 10:53:51 -07:00
enum Foo(Zero, One(x:Int), Two(x:Int, y:Int), Three(x:Int, y:Text, z:Bool), Four(x,y,z,w:Int), Last(t:Text))
2025-04-06 13:07:23 -07:00
func choose_text(f:Foo->Text)
2024-05-01 10:53:51 -07:00
>> f
2025-04-06 13:07:23 -07:00
when f is Zero
2024-05-01 10:53:51 -07:00
return "Zero"
2025-04-06 13:07:23 -07:00
is One(one)
return "One: $one"
2025-04-06 13:07:23 -07:00
is Two(x, y)
return "Two: x=$x, y=$y"
2025-04-06 13:07:23 -07:00
is Three(three)
return "Three: $three"
2025-04-06 13:07:23 -07:00
is Four
2024-05-01 10:53:51 -07:00
return "Four"
2025-04-06 13:07:23 -07:00
else
return "else: $f"
2024-02-24 12:24:44 -08:00
2025-04-06 13:07:23 -07:00
func main()
2024-04-12 10:09:31 -07:00
>> Foo.Zero
= Foo.Zero
2024-04-12 10:09:31 -07:00
>> Foo.One(123)
= Foo.One(123)
2024-04-12 10:09:31 -07:00
>> Foo.Two(123, 456)
= Foo.Two(x=123, y=456)
2024-04-12 10:09:31 -07:00
>> one := Foo.One(123)
>> one.One
= yes
>> one.Two
= no
2024-04-12 10:09:31 -07:00
>> Foo.One(10) == Foo.One(10)
= yes
>> Foo.One(10) == Foo.Zero
= no
>> Foo.One(10) == Foo.One(-1)
= no
>> Foo.One(10) < Foo.Two(1, 2)
= yes
>> x := Foo.One(123)
2025-04-06 13:34:23 -07:00
>> t := |x|
>> t.has(x)
= yes
>> t.has(Foo.Zero)
= no
2024-04-12 10:09:31 -07:00
2024-05-01 10:53:51 -07:00
>> choose_text(Foo.Zero)
= "Zero"
>> choose_text(Foo.One(123))
= "One: 123"
>> choose_text(Foo.Two(123, 456))
= "Two: x=123, y=456"
>> choose_text(Foo.Three(123, "hi", yes))
= 'Three: Three(x=123, y="hi", z=yes)'
2024-05-01 10:53:51 -07:00
>> choose_text(Foo.Four(1,2,3,4))
= "Four"
>> choose_text(Foo.Last("XX"))
= 'else: Last("XX")'
2024-03-17 18:48:53 -07:00
2024-05-23 09:40:21 -07:00
i := 1
cases := [Foo.One(1), Foo.One(2), Foo.Zero]
2025-04-06 13:07:23 -07:00
while when cases[i] is One(x)
2024-05-23 09:40:21 -07:00
>> x
i += 1
>> [
(
2025-04-06 13:07:23 -07:00
when x is One(y), Two(y,_)
"Small $y"
2025-04-06 13:07:23 -07:00
is Zero
"Zero"
2025-04-06 13:07:23 -07:00
else
"Other"
) for x in [Foo.Zero, Foo.One(1), Foo.Two(2,2), Foo.Three(3,"",no)]
]
= ["Zero", "Small 1", "Small 2", "Other"]
2025-04-06 13:07:23 -07:00
>> expr := when cases[1] is One(y)
y + 1
2025-04-06 13:07:23 -07:00
else
-1
= 2