Stream: ideas

Topic: pattern match if


view this post on Zulip Matthias Devlamynck (Jan 14 2022 at 15:33):

I think it would be nice if Roc supported some form of pattern matching in conditions like isOk = \result -> result == Ok _ to avoid the need for when … is ….
Rust has if let <pattern> = <value> {} syntax and sometimes I missed it in elm.
Might not be a feature to make the language cleaner though. Most of the time a helper function would be cleaner.

What do you think?

view this post on Zulip Lucas Rosa (Jan 14 2022 at 15:57):

That's pretty much just:

when <value> is
   <pattern> -> ...
   _ -> ...

view this post on Zulip Lucas Rosa (Jan 14 2022 at 15:57):

in roc I'm pretty sure every if needs an else branch

view this post on Zulip Lucas Rosa (Jan 14 2022 at 15:59):

so this situation if it was implemented could just be sugar for that when, but the if would still need an else for the catch all case

view this post on Zulip Matthias Devlamynck (Jan 14 2022 at 16:32):

You're right I wasn't precise enought. My use case actually doesn't directly imply ifs. It would rather be an operator (or extension of ==) :
result == Ok _ or result ~= Ok _ if we want to use a different operator.

when result is
   Ok _ -> True
   _ -> False

view this post on Zulip Lucas Rosa (Jan 14 2022 at 20:18):

oh cool, that reminds me of what you can do with elixir

view this post on Zulip Zeljko Nesic (Jan 14 2022 at 20:54):

This basically hint for the need of a Hole!

view this post on Zulip Zeljko Nesic (Jan 14 2022 at 20:54):

_ This is the hole

view this post on Zulip Zeljko Nesic (Jan 14 2022 at 20:54):

and everything works in the hole


Last updated: Jun 16 2026 at 16:19 UTC