-1

Is a wildcard always required for union cases of Active Patterns?

Warning:

Incomplete pattern matches on this expression.

let (|Positive|Neutral|Negative|) = function
    | x when x > 0 -> Positive
    | x when x = 0 -> Neutral
    | x when x < 0 -> Negative

The warning is resolved when I insert a wild card as a union case:

let (|Positive|Neutral|Negative|) = function
    | x when x > 0 -> Positive
    | x when x = 0 -> Neutral
    | x when x < 0 -> Negative
    | _            -> failwith "unknown"

What other cases am I missing?

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • You should read the F# specification section 7.2.3 Active Patterns – Guy Coder Apr 07 '16 at 14:45
  • Thanks. I reviewed it. However, I did not see the information regarding the warning that I was receiving. – Scott Nimrod Apr 07 '16 at 15:03
  • 1
    Possible duplicate of [F# Incomplete pattern matches on this expression when using "when"..Why?](http://stackoverflow.com/questions/18691622/f-incomplete-pattern-matches-on-this-expression-when-using-when-why) – Ringil Apr 07 '16 at 15:24

1 Answers1

4

when guards can't be checked (in general) by the compiler. In this particular case though, just leave out the last guard as you are not missing a case:

let (|Positive|Neutral|Negative|) = function
    | x when x > 0 -> Positive
    | x when x < 0 -> Negative
    | _ -> Neutral

Note: for int, this is OK, floats need a few cases more (NaN and infinity) and the compiler won't help you there (as you just discovered).

CaringDev
  • 8,391
  • 1
  • 24
  • 43