Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci_scripts/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fi

# opt-in list of examples to test (add examples as they are updated for the new compiler)
optin=(
"Tuples"
)

is_optin() {
Expand Down Expand Up @@ -101,7 +102,6 @@ if is_optin "TryOperatorDesugaring"; then
fi

if is_optin "Tuples"; then
$ROC build ./examples/Tuples/main.roc
expect ci_scripts/expect_scripts/Tuples.exp
fi

Expand Down
1 change: 1 addition & 0 deletions ci_scripts/check_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fi

# opt-in list of files to format check (add files as they are updated for the new compiler)
optin=(
"examples/Tuples/main.roc"
)

for file in "${optin[@]}"; do
Expand Down
4 changes: 2 additions & 2 deletions ci_scripts/expect_scripts/Tuples.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ set timeout 7

source ./ci_scripts/expect_scripts/shared-code.exp

spawn ./examples/Tuples/main
spawn $env(ROC) ./examples/Tuples/main.roc --no-cache

expect "First is: A String,\r\nSecond is: true,\r\nThird is: 15000000.\r\nYou also have some pears.\r\n" {
expect "First is: Just a String,\r\nSecond is: true,\r\nThird is: 15000000.0.\r\n\r\nYou have some pears.\r\n" {
expect eof {
check_exit_and_segfault
}
Expand Down
7 changes: 4 additions & 3 deletions examples/Tuples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ For example, instead of having `foo.name` to access the `name` field of a record
you might write `foo.0` to access the first field in a tuple (or `foo.1` for the second
field, etc.)


## Code

```roc
file:main.roc
```
Expand All @@ -17,8 +17,9 @@ Run this from the directory that has `main.roc` in it:

```
$ roc main.roc
First is: A String,
First is: Just a String,
Second is: true,
Third is: 15000000.
Third is: 15000000.0.

You also have some pears.
```
75 changes: 38 additions & 37 deletions examples/Tuples/main.roc
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br" }

import pf.Stdout
import pf.Arg exposing [Arg]

main! : List Arg => Result {} _
main! = |_args|

# a tuple that contains three different types
simple_tuple : (Str, Bool, I64)
simple_tuple = ("A String", Bool.true, 15_000_000)

# access the items in a tuple by index (starts at 0)
first_item = simple_tuple.0
second_item = if simple_tuple.1 then "true" else "false"
third_item = Num.to_str(simple_tuple.2)

Stdout.line!(
"""
First is: ${first_item},
Second is: ${second_item},
Third is: ${third_item}.
""",
)?

# You can also use tuples with `when`:
fruit_selection : [Apple, Pear, Banana]
fruit_selection = Pear

quantity = 12

when (fruit_selection, quantity) is
# TODO re-enable when github.com/roc-lang/roc/issues/5530 is fixed.
# (_, qty) if qty == 0 -> Stdout.line! "You have no fruit."
(Apple, _) -> Stdout.line!("You also have some apples.")
(Pear, _) -> Stdout.line!("You also have some pears.")
(Banana, _) -> Stdout.line!("You also have some bananas.")
main! : List(Str) => Try({}, _)
main! = |_args| {

# A tuple that contains three different types
simple_tuple : (Str, Bool, Dec)
simple_tuple = ("Just a String", True, 15_000_000)

# Access the items in a tuple by index (starts at 0)
first_item = simple_tuple.0
second_item = if simple_tuple.1 "true" else "false"
third_item = simple_tuple.2.to_str()

echo!(
\\First is: ${first_item},
\\Second is: ${second_item},
\\Third is: ${third_item}.
\\
,
)

# You can also use tuples with `match`:
fruit_selection : [Apple, Pear, Banana]
fruit_selection = Pear

quantity = 12

# Create a tuple of fruites and quantities,
# So you can match on both of them
match (fruit_selection, quantity) {
(_, 0) => echo!("You have no fruit.")
(Apple, 1) => echo!("You have an apple.")
(Apple, _) => echo!("You have some apples.")
(Pear, _) => echo!("You have some pears.")
(Banana, _) => echo!("You have some bananas.")
}

Ok({})
}