Stream: ideas

Topic: single line pattern matching


view this post on Zulip Matthias Toepp (May 10 2023 at 12:20):

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 ... is has no single-line form)

view this post on Zulip Matthias Toepp (May 10 2023 at 12:25):

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.)

view this post on Zulip Matthias Toepp (May 10 2023 at 12:27):

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")

view this post on Zulip Matthias Toepp (May 10 2023 at 12:29):

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.

view this post on Zulip Richard Feldman (May 10 2023 at 13:30):

I'd like to discuss this idea decoupled from any other proposals

view this post on Zulip Richard Feldman (May 10 2023 at 13:32):

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

view this post on Zulip Richard Feldman (May 10 2023 at 13:32):

I wonder if there are any styles of writing this would enable that are desirable?

view this post on Zulip Richard Feldman (May 10 2023 at 13:32):

e.g. has anyone run into wanting single-line when in Roc today?

view this post on Zulip Brendan Hansknecht (May 10 2023 at 13:50):

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.

view this post on Zulip Matthias Toepp (May 10 2023 at 13:54):

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