Stream: ideas

Topic: Prevent `else if`


view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:28):

Edit: Some interesting new solutions are being generated below but I put this out here too quickly...it was poorly thought through, read on only if you wish to discover my foolishness...

Would it be a good to have the compiler put up some kind of barrier to chaining with else if?
It could suggest that matching should be used instead, given that...

greeting =
    match True:
        num < 10 ->
            "less than 10"
        num < 100 ->
            "less than 100"
        num < 1000 ->
            "less than 1000"
        _ ->
            "it's a big number"

is much cleaner than:

greeting =
    if num < 10 then
        "less than 10"
    else if num < 100 then
        "less than 100"
    else if num < 1000 then
        "less than 1000"
    else
        "it's a big number"

so this implies changing when .. is to match..: so it sounds better to match on True. And it could also mean that True would have to be represented as True and not Bool.true.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:35):

Note: It seems like if..then..else can't be removed altogether because single line if statements are desirable. Also possibly becauseif..then..else is more elegant comparet to pattern matching for the simplest case of working with bools.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:43):

To express the problem with else if in words:

  1. It pushes the condition five characters past the ideal of having vertical alignment of all the conditions in an if..else if block. So that breaks the flow when visually scanning the conditions vertically.
  2. The noise of seeing two unnecessary extra words obscuring the conditions , when the conditions are the key points of interest.

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

Isn't this just the alternate if syntax but slightly reworded? Not saying we can't improve when to work better with guards, but I think this is identical to the alternate syntax, just with match True instead of if.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:49):

I'm suggesting we

  1. use a single pattern matching syntax for:
    1. many-branched would-beelse ifchains
    2. regular pattern matching.
  2. And also implement a deterrent to using else if
  3. Renaming when..is to match..:

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

Question on the syntax: so it is essentially written in reverse form of current when ... is. What i mean by that is, in

when x is
   12 -> ...

x is always an expression and 12 is always a constant, though it is a special pattern matching constant that may bind variables.

Do you foresee any issue with merging these two syntaxes and the potential extra flexibility it adds? Theoretically, this could be used with any value, and not just Bool.true.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:56):

i'm not meaning to suggest that...

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:57):

it's functionally the same, but changing the word choice from when..is to match..:

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:57):

maybe i'm misunderstanding something...let me read your post again.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:58):

i see what you're saying

view this post on Zulip Matthias Toepp (Apr 20 2023 at 14:59):

yes so it is a special syntax...not as good an idea as I had hoped.

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

For reference, the current equivalent syntax is something like:

greeting =
    when Bool.true is# Note: this could be any expression, does not have to be true.
        _ if num < 10 ->
            "less than 10"
        _ if num < 100 ->
            "less than 100"
        _ if num < 1000 ->
            "less than 1000"
        _ ->
            "it's a big number"

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

That is why I made the comment on guards.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:02):

yup that works but it's obviously kind of ugly

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

for sure

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:02):

I withdraw the idea. (sorry)

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

Always good to talk about these things. Theoretically, patterning matching could be made more flexible to support this kind of matching. I'm just not sure the ramifications of something like that.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:03):

@Brendan Hansknecht
Thanks for setting me straight.

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:10):

Well... what if we did make it so that if the tag True was passed to the match statememt that would cause it to evaluate boolean expressions? Anyway it's probably not a good idea.

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

That definitely would give you the feature you want. It may seem a bit out of place if it is the only special case. I have no idea what the implementation complexity would be given we now need specific bool expressions instead of patterns matching constants that may bind variables.

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

Definitely sounds doable

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:23):

but we're back into strange territory

view this post on Zulip Matthias Toepp (Apr 20 2023 at 15:24):

...this always seems so close and yet so far!

view this post on Zulip Georges Boris (Apr 20 2023 at 19:26):

My current thought process fwiw :)

I don't think there was any downsides for the proposed syntax in the cond thread if we went for a syntax based on pattern matching. the downsides are related to the approach itself - It prevented single line usages and increased horizontal indentation.

Just think the language designers thought it was not better enough to justify not using the common if/then/else statement. I think the bar for changing this syntax is quite high since it is not seen as a problem, more like stylistic taste and some people don't really mind the verbosity since it's familiar.

So if we want to propose something new I think it needs to be a new approach entirely that either prevents serious problems with the current implementation (that we have not found yet) or gives us some super powers that would be a superset of the current feature. Even then, maybe it would still be decided to keep if as-is in addition to this new super power feature, since it doesn't hurt to have that familiar feature also available for common use cases.

view this post on Zulip Richard Feldman (Apr 20 2023 at 19:38):

yeah I don't know if we've talked about it here, but one thing I've seen discussed in the Elm community is just: why have if at all?

view this post on Zulip Richard Feldman (Apr 20 2023 at 19:38):

it's completely redundant with (in Roc terms) when

view this post on Zulip Richard Feldman (Apr 20 2023 at 19:45):

arguments for if include:

view this post on Zulip Richard Feldman (Apr 20 2023 at 19:45):

a thread about this in Elm Discourse: https://discourse.elm-lang.org/t/if-then-else-versus-case-expressions/2273

view this post on Zulip Matthias Toepp (Apr 21 2023 at 10:39):

Something like that could be really cool!

If we adjusted the meaning of else to be the last decision
in a decision structure...

Then we could have the following:

if x < 10 then "small" else "big"

if  x < 10 then
        "small"
    else
        "big"

if  x < 10 then
        "less than ten"
    x < 100 then
        "less than 100"
    x < 1000 then
        "less than 1000"
    else
        "big"

when val is
    LessThenTen ->
        "small"
    else
        "big"

view this post on Zulip Matthias Toepp (Apr 21 2023 at 10:42):

I'll start a new thread for that last idea. I'll link back from there...

view this post on Zulip Matthias Toepp (Apr 21 2023 at 10:51):

The idea of "adjusting the meaning of else" ...
https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/adjust.20meaning.20of.20else


Last updated: Jun 16 2026 at 16:19 UTC