Question

Given the following active pattern:

let (| HasMatch |) (x:string) = 
  if x.Contains("0") then Some()
  else None;;

And the following pattern matching func:

let testFn x = function
  | HasMatch i -> printfn "%A" i
  | _ -> printf "nope";;

The last line's wildcard pattern says warning FS0026: This rule will never be matched

All of the examples i see seem to infer that partial active patterns must return Some('a) to match, and that ones that return None get captured by the wildcard. The error seems to say differently.

What am i missing?

Was it helpful?

Solution

I think you should add the None case to the active pattern declaration as follows:

let (| HasMatch | _ |) (x:string) = 
  if x.Contains("0") then Some()
  else None;;

In your orignal example, the compiler infers that you actually want to return the Option type. When you run the printf in your example, you would see it print Some Null when there is a match.

Also, it is bad to return Some(), you should return say Some(x) or similar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top