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!
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.
Actually the syntax (parsing/formatting) works fine - it’s the rest of the compiler that doesn’t support tuples yet.
To an end user, that is that the same thing, but I get the technicality.
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"
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."
You can pattern match tuples. The issue with your first example is that the roc currently can't pattern match bools.
If those were tags instead of bools, it would work
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)
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
FWIW I ran across precisely the same issue, and yeah, the error message wasn't helpful here.
Ah of course! :man_facepalming:
Can you file an issue for a better error message @Baffalop?
Funny, i ran into the same problem.
Last updated: Jul 05 2025 at 12:14 UTC