Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multiple match cases with single statement in Elm

Tags:

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"
}
like image 477
Max Semikin Avatar asked Dec 13 '16 09:12

Max Semikin


1 Answers

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
        ...
like image 144
Chad Gilbert Avatar answered Oct 27 '22 00:10

Chad Gilbert