Danny Ellis Jr.
Danny Ellis Jr.

Reputation: 1706

what is the syntax for a negative case statement in elm?

I need to know how to do something like this in ELM.

case form.mode of
     "" ->  modeDropdown form

     (not "solo") -> do something for all other possibilities

Is this even possible?

EDIT:

To give broader context, I am trying to create cascading down-down lists. If I receive "" for mode in the url parameters, I need to show the first drop-down list. If I get "solo", I do not need to show anything. If I get any of the other choices besides "solo", I have to display the second drop-down list. It occurred to me that I can return an empty span tag for "solo" but that feels a little hacky.

Upvotes: 1

Views: 424

Answers (1)

Igor Drozdov
Igor Drozdov

Reputation: 15045

Is this even possible?

The way it described in the question - no, because a pattern need to be provided in a case expression, not a boolean condition. But it can be performed the following way:

case form.mode of
    "" ->  modeDropdown form
    "solo" ->
        -- do something for `"solo"` possibility
     _ ->
        -- do something for all other possibilities

This way you provide exhaustive pattern matching, i. e. all the cases are considered.

Regarding the questions in the comment section:

The Haskell syntax would be s | s /= "solo" -> do stuff. Does Elm have anything like that?

There is no syntax for functions with guards in Elm, so you need to imitate this behaviour using if conditions. Or there is elm-guards library, which makes it less verbose.

Upvotes: 5

Related Questions