Stream: ideas

Topic: expand `when` to support expressions


view this post on Zulip Brendan Hansknecht (Apr 20 2023 at 20:21):

I think that when with full expression support instead of just constants with binding variables could potentially enable super powers, but I am not sure if it would be reasonable. Likely it would enable a large set of confusing code, but I am not really sure cause I haven't messed with it enough. A few examples off the top of my head:

Be used instead of if as mentioned above:

when Bool.true is
    num < 10 ->
        "less than 10"
    num < 100 ->
        "less than 100"
    num < 1000 ->
        "less than 1000"
    _ ->
        "it's a big number"

try multiple things until the first Ok result and then do something.
Each Ok can be handled different in each branch.

when Ok _ is
    List.get someList 3 ->
       ...
    Num.addChecked x y ->
        ...
    somePossiblyFailingFunction a b ->
        ...
    _ ->
        crash "everything failed"

Theoretically if the types line up, you could write when Ok val is for the first line of the above example. That would enable still binding the wrapped value and using it in sub branches. I guess this is another way to write a chaining of results with special handling on failure, but it is still intriguing.

I assume something of this nature would actually be very powerful and interesting, but my gut feeling is that it would be too powerful and lead to more confusing code rather than be positive overall.

view this post on Zulip Georges Boris (Apr 21 2023 at 10:19):

@Brendan Hansknecht great points, I'm sold.

the second example is "woah very interesting!! I hate it!" haha but you just opened up a box of possibilities in my head :)

view this post on Zulip Notification Bot (Apr 21 2023 at 14:53):

A message was moved here from #ideas > Prevent else if by Brendan Hansknecht.

view this post on Zulip Brendan Hansknecht (Apr 21 2023 at 14:55):

Just throwing this in it's own topic so that it can be explored some. As I said above, I think it is likely too powerful, but it is an interesting idea and may spark some real use cases for people

view this post on Zulip dank (Apr 24 2023 at 18:43):

sorry mate but it just feels so wrong lol


Last updated: Jun 16 2026 at 16:19 UTC