|
1 | | -app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br" } |
2 | | - |
3 | | -import pf.Stdout |
4 | | -import pf.Arg exposing [Arg] |
5 | | - |
6 | | -main! : List Arg => Result {} _ |
7 | | -main! = |_args| |
8 | | - |
9 | | - # a tuple that contains three different types |
10 | | - simple_tuple : (Str, Bool, I64) |
11 | | - simple_tuple = ("A String", Bool.true, 15_000_000) |
12 | | - |
13 | | - # access the items in a tuple by index (starts at 0) |
14 | | - first_item = simple_tuple.0 |
15 | | - second_item = if simple_tuple.1 then "true" else "false" |
16 | | - third_item = Num.to_str(simple_tuple.2) |
17 | | - |
18 | | - Stdout.line!( |
19 | | - """ |
20 | | - First is: ${first_item}, |
21 | | - Second is: ${second_item}, |
22 | | - Third is: ${third_item}. |
23 | | - """, |
24 | | - )? |
25 | | - |
26 | | - # You can also use tuples with `when`: |
27 | | - fruit_selection : [Apple, Pear, Banana] |
28 | | - fruit_selection = Pear |
29 | | - |
30 | | - quantity = 12 |
31 | | - |
32 | | - when (fruit_selection, quantity) is |
33 | | - # TODO re-enable when github.com/roc-lang/roc/issues/5530 is fixed. |
34 | | - # (_, qty) if qty == 0 -> Stdout.line! "You have no fruit." |
35 | | - (Apple, _) -> Stdout.line!("You also have some apples.") |
36 | | - (Pear, _) -> Stdout.line!("You also have some pears.") |
37 | | - (Banana, _) -> Stdout.line!("You also have some bananas.") |
| 1 | +main! : List(Str) => Try({}, _) |
| 2 | +main! = |_args| { |
| 3 | + |
| 4 | + # A tuple that contains three different types |
| 5 | + simple_tuple : (Str, Bool, Dec) |
| 6 | + simple_tuple = ("Just a String", True, 15_000_000) |
| 7 | + |
| 8 | + |
| 9 | + # Access the items in a tuple by index (starts at 0) |
| 10 | + first_item = simple_tuple.0 |
| 11 | + second_item = if simple_tuple.1 "true" else "false" |
| 12 | + third_item = simple_tuple.2.to_str() |
| 13 | + |
| 14 | + echo!( |
| 15 | + \\First is: ${first_item}, |
| 16 | + \\Second is: ${second_item}, |
| 17 | + \\Third is: ${third_item}. |
| 18 | + \\ |
| 19 | + ) |
| 20 | + |
| 21 | + # You can also use tuples with `match`: |
| 22 | + fruit_selection : [Apple, Pear, Banana] |
| 23 | + fruit_selection = Pear |
| 24 | + |
| 25 | + quantity = 12 |
| 26 | + |
| 27 | + # Create a tuple of fruites and quantities, |
| 28 | + # So you can match on both of them |
| 29 | + match (fruit_selection, quantity) { |
| 30 | + (_, 0) => echo!("You have no fruit.") |
| 31 | + (Apple, 1) => echo!("You have an apple.") |
| 32 | + (Apple, _) => echo!("You also have some apples.") |
| 33 | + (Pear, _) => echo!("You also have some pears.") |
| 34 | + (Banana, _) => echo!("You also have some bananas.") |
| 35 | + } |
| 36 | + |
| 37 | + Ok({}) |
| 38 | +} |
0 commit comments