Search code examples
elm

Handling multiple match cases with single statement in Elm


I know in Scala you can handle multiple patterns with a single expression, is something like this possible in Elm?

l match {
    case B(_) | C(_) => "B"
}

Solution

  • In Elm, you can only match upon one pattern at a time, unless you are pattern matching on the underscore character, which catches all.

    case l of
        B _ -> "B"
        C _ -> "B"
        ...
    
    -- or...
    case l of
        ...
        _ -> "B"
    

    If you have something more complex that a string, it is probably best to pull it into its own function:

    let
        doB -> "B"
    in
        case l of
            B _ -> doB
            C _ -> doB
            ...