14

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"
}
Max Semikin
  • 974
  • 1
  • 11
  • 19

2 Answers2

13

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
        ...
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
0

You can do something similar to the following

sample expression = 
    case expression of
        Case1 -> "String"
        Case2 -> sample Case1
        Case3 -> sample Case1
Hari Roshan
  • 344
  • 4
  • 10