Shorter else if syntax

I think the syntax for if statements

if n > 0 then
    "positive"
else if n < 0 then
    "negative"
else
    "zero"

should be changed to this.

if n > 0 then
    "positive"
if n < 0 then
    "negative"
else
    "zero"

It removes an unnecessary inconsistency between the first and rest of the conditions, reduces line length, and makes it easier to reorder conditions.

This adds a new syntax for “else if”. Currently, there is no special syntax, and you can next if expressions.

I personally think that if statements should be removed entirely because they are redundant with case statements.

1 Like

This would be a good idea. It won’t force people to use messy nested case statements because helper functions can be used with pipes:

justIf : (() -> Bool) -> a -> Maybe a -> Maybe a
justIf condition value maybe =
    case maybe of
        Just a -> Just a
        Nothing -> case condition () of
            Just a -> Just a
            Nothing -> Nothing

Nothing
    |> justIf (\_ -> n > 0)
        "positive"
    |> justIf (\_ -> n < 0)
        "negative"
    |> Maybe.withDefault
        "zero"

This syntax is possibly ambiguous though

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.