Stream: beginners

Topic: Pattern matching tuples


view this post on Zulip Tankor Smash (Jan 18 2023 at 16:21):

Hey, I was trying to write a pattern match for a tuple (when (left, right) is), and the compiler threw an error:

thread '<unnamed>' panicked at 'not yet implemented: desugar_expr: Tuple', crates/compiler/can/src/operator.rs:180:13

I was able to figure out that using a list works fine ( when [left, right] is).

Not sure how feasible it is to do, but having that be a nicer error message would be neat!

view this post on Zulip Brendan Hansknecht (Jan 18 2023 at 18:13):

I think the core issue is that we don't actually have native tuple syntax yet. So there is no (left, right) syntax. Instead, you would have to make the tuple explicitly with a tag: T left right where T is just an arbitrary tag name. Could be Pair or Tuple or T or etc.

view this post on Zulip Joshua Warner (Jan 19 2023 at 15:31):

Actually the syntax (parsing/formatting) works fine - it’s the rest of the compiler that doesn’t support tuples yet.

view this post on Zulip Brendan Hansknecht (Jan 19 2023 at 15:56):

To an end user, that is that the same thing, but I get the technicality.

view this post on Zulip Nathan Kramer (Oct 03 2024 at 08:20):

Hi, I'm guessing from reading the above messages that I can't pattern match tuples like I'm trying to in this code?

response : Str -> Str
response = \heyBob ->
    isQuestion = Str.endsWith heyBob "?"
    isSilence = Str.trim heyBob == ""
    isYelling = strIsYelling heyBob
    when (isQuestion, isYelling, isSilence) is
        (Bool.true, Bool.true, Bool.false) -> "Calm down, I know what I'm doing!"
        (Bool.true, Bool.false, Bool.false) -> "Sure."
        (Bool.false, Bool.true, Bool.false) -> "Whoa, chill out!"
        (_, _, Bool.true) -> "Fine. Be that way!"
        _ -> "Whatever"

view this post on Zulip Nathan Kramer (Oct 03 2024 at 08:37):

I ended up re-writing it like this

response : Str -> Str
response = \heyBob ->
    sanitized = Str.trim heyBob
    isQuestion = Str.endsWith sanitized "?"
    isSilence = sanitized == ""
    isYelling = strIsYelling sanitized

    if isSilence then
        "Fine. Be that way!"
    else if isQuestion && isYelling then
        "Calm down, I know what I'm doing!"
    else if isQuestion then
        "Sure."
    else if isYelling then
        "Whoa, chill out!"
    else
        "Whatever."

view this post on Zulip Brendan Hansknecht (Oct 03 2024 at 15:05):

You can pattern match tuples. The issue with your first example is that the roc currently can't pattern match bools.

view this post on Zulip Brendan Hansknecht (Oct 03 2024 at 15:05):

If those were tags instead of bools, it would work

view this post on Zulip Baffalop (Dec 01 2024 at 23:24):

Hey all, my first time writing a Roc program for AoC day 1. Is this a parser bug or am I missing something? The tutorial suggested you can pattern match tuples, but this doesn't seem to be the case in function parameters?

unzip : List (a, b) -> (List a, List b)
unzip = \pairs ->
    pairs |> List.walk ([], []) \(as, bs), (a, b) ->
        (List.append as a, List.append bs b)

yields the error:

I am partway through parsing a pattern in parentheses, but I got stuck
here:

33│      pairs |> List.walk ([], []) \(as, bs), (a, b) ->
                                       ^

I was expecting to see a closing parenthesis before this, so try
adding a ) and see if that helps?

but the following compiles:

unzip : List (a, b) -> (List a, List b)
unzip = \pairs ->
    pairs |> List.walk ([], []) \res, pair ->
        (List.append res.0 pair.0, List.append res.1 pair.1)

view this post on Zulip Eli Dowling (Dec 02 2024 at 03:47):

Hey @Baffalop This really stumped me for a minute. I was looking at my own code and seeing where I have an unzip function that is almost identical.
The issue is, you are using the as keyword as a name. This should have a much better error message, but that is the issue

view this post on Zulip Matt Harden (Dec 02 2024 at 04:48):

FWIW I ran across precisely the same issue, and yeah, the error message wasn't helpful here.

view this post on Zulip Baffalop (Dec 02 2024 at 07:39):

Ah of course! :man_facepalming:

view this post on Zulip Anton (Dec 02 2024 at 11:13):

Can you file an issue for a better error message @Baffalop?

view this post on Zulip Eelco Hoekema (Dec 04 2024 at 19:25):

Funny, i ran into the same problem.


Last updated: Jul 05 2025 at 12:14 UTC