Search code examples
pattern-matchingelm

Does "case .. of" supports a fallthrough in Elm?


Javascript's switch supports a fallthrough:

function update(action, model) {
  switch (action) {
    case SHUFFLE:
      return shuffle(model);

    case MOVE_LEFT:
    case MOVE_RIGHT:
    case MOVE_UP:
    case MOVE_DOWN:
      return move(action, model);

    default:
      return model;
  }
}

How would you implement this in Elm?


Solution

  • I would model it like this:

    type Direction = Left | Right | Up | Down
    
    
    type Action = Shuffle | Move Direction 
    
    
    update action model =
      case action of 
        Shuffle -> shuffle model 
        Move dir -> move dir model 
    

    Elm's case does not have fall-through.

    "a case-expression does not have fall through, so you don't need to say break everywhere to make things sane."

    http://elm-lang.org/guide/model-the-problem