This started from https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/pattern.20matching.20.60if.60.2C.20no.3A.20.60then.60.20.60else.60
:
Richard Feldman said:
size = if x > 5 is (True -> "greater") (False -> "smaller")this is an interesting syntax idea for single-line pattern matches I haven't seen before - maybe it's worth a separate discussion? (e.g. current
when...ishas no single-line form)
My interest is that it could simplify roc and make it more powerful at the same time. One line pattern matching is more powerful than our current single-line if, and opens the possibility of eliminating the else then construct (In the link above I am proposing renaming when to if among other things.)
In terms of the current pattern matching syntax of when is this would be the equivalent of:
size = when x > 5 is (True -> "greater") (False -> "smaller")
In terms of displacing if then else, as shown, it raises the issue of pattern matching on bools currently not being possible in roc.
However this is in the works: https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/expose.20Bool.2Etrue.20as.20True at least for Bool.true and Bool.false, and one could hope that Bool.true and Bool.false would be exposed as true and false if not True and False.
I'd like to discuss this idea decoupled from any other proposals
so today, when can only be used in multi-line form. Rust has some single-line pattern matching options (e.g. if let and matches!) which aren't quite the same as this, but which see quite a bit of use in Rust
I wonder if there are any styles of writing this would enable that are desirable?
e.g. has anyone run into wanting single-line when in Roc today?
I feel like I hit wanting it a lot more in rust. Not sure why. Maybe just a difference in the kind of code I have worked in Roc vs Rust.
My only immediate thought are things like Result.isOk. I assume that would come up for other tags. Roc would promote writing that as a helper function rather than just going matches data (SomeTag _) Or whatever other syntax.
Presumably it would be desirable for the same/similar reasons as one line if, whatever those reasons are.
Last updated: Jun 16 2026 at 16:19 UTC