Pattern matching with guard clauses

344 views
Skip to first unread message

Sathish Kumar

unread,
Feb 19, 2016, 3:20:11 PM2/19/16
to Elm Discuss
Hi all,
Does Elm allow pattern matching with a guard clause?
I could not find something like this in syntax (http://elm-lang.org/docs/syntax)

take k xs =
  case (k, xs) of
    (k, xs) if k == 0 -> Ok []
    (k, xs) if k < 0 -> Err ("negative index")
    (k, []) -> Err "insufficient elements)
    (k, xs) -> ....

Joey Eremondi

unread,
Feb 19, 2016, 3:36:25 PM2/19/16
to elm-d...@googlegroups.com
No, Elm does not currently allow this.
In particular, Elm requires all matches to be exhaustive, and such clauses would make it much more difficult to analyze exhaustiveness.
There have been some requests to have it implemented, but it's definitely not on the top of the list for future Elm features.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

unread,
Feb 19, 2016, 6:23:11 PM2/19/16
to Elm Discuss
You either want to place the if-statement as the result of the pattern match, or to call out to a function.
Reply all
Reply to author
Forward
0 new messages