I have had this idea for a while and think even in case it won't be implemented because the drawbacks outweigh the benefits, the idea itself is still cool. So, allow me to present:
if condition1 {
"1"
} else if condition2 {
"2"
} else if condition3 {
"3"
} else {
"4"
}
An alternative to chains of if-else blocks.
when {
condition1 => "1"
condition2 => "2"
condition3 => "3"
_ => "4"
}
The intended semantics of the when keyword would be sequential evaluation, returning the first value for which the condition is true.
Conceptually, you can think of this as syntactic sugar over if-else chains, or even nested match expressions like so:
match condition1 {
True => "1"
False => match condition2 {
True => "2"
False => match condition3 {
True => "3"
False => "4"
}
}
}
But as you can see, using the match keyword for this purpose is very verbose and for certain not considered good practice.
This feature is purely an addition of syntactical capability.
Could potentially a) replace the if-else keywords, or b) live alongside it.
a) seems unlikely to me because of Roc's goal to be friendly, which also means easy to learn. Familiarity is a comfort and, perhaps the inclusions of the traditional keywords like for and allowing explicit mutability is arguably very important for this goal of friendliness, considering the massive leaps Roc already takes to innovate in computer science.
b) could happen. Now, this wouldn't be keeping to a minimal keyword-set, and perhaps that is also undesirable. The feature is specifically less verbose for when multiple conditions need to be sequentially evaluated and can't be in a match expression, since it's not an exhaustable set of variants you need to check for.
When expressions always need to be terminated by an underscore or Bool.True as to avoid undefined behavior.
Anyway, I just love Roc and wanted to get this idea out of my head, so, thanks for your time if you ended up reading all this :)
We've talked about this a long time ago. The Gleam language does this if I remember correctly, ditching if expressions all together. We've concluded it's not the right fit for Roc (among others there's unfamiliarity you've also listed). Now that there are standalone if statements with return statements in them, it wouldn't be possible to do this, becauseif expressions no longer require exhaustive conditions (having an else branch) and the point of match is to be exhaustive.
Gotta love the elegance of the idea though :)
Oh, I didn't know about that change! Makes sense, that the return keyword would no longer require having else block.
Thanks for the consideration :D
I should mention I was inspired by Kotlin :)
Heard good things about Kotlin, but don't know much about it.
I think this was the discussion (older, so it's for the legacy syntax when "match thing" was "when thing is"): #ideas > ✔ 1 line pattern match + `when is`-like bool structure
Last updated: Jun 16 2026 at 16:19 UTC