5

Suppose I have the following code:

type Vehicle =
| Car  of string * int
| Bike of string

let xs = [ Car("family", 8); Bike("racing"); Car("sports", 2); Bike("chopper") ]

I can filter above list using incomplete pattern matching in an imperative for loop like:

> for Car(kind, _) in xs do
>    printfn "found %s" kind;;

found family
found sports
val it : unit = ()

but it will cause a:warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Bike (_)' may indicate a case not covered by the pattern(s). Unmatched elements will be ignored.

As the ignoring of unmatched elements is my intention, is there a possibility to get rid of this warning?

And is there a way to make this work with list-comprehensions without causing a MatchFailureException? e.g. something like that:

> [for Car(_, seats) in xs -> seats] |> List.sum;;
val it : int = 10
Alexander Battisti
  • 2,178
  • 2
  • 19
  • 24

3 Answers3

10

Two years ago, your code was valid and it was the standard way to do it. Then, the language has been cleaned up and the design decision was to favour the explicit syntax. For this reason, I think it's not a good idea to ignore the warning.

The standard replacement for your code is:

for x in xs do
    match x with
    | Car(kind, _) -> printfn "found %s" kind
    | _ -> ()

(you could also use high-order functions has in pad sample)

For the other one, List.sumBy would fit well:

xs |> List.sumBy (function Car(_, seats) -> seats | _ -> 0)

If you prefer to stick with comprehensions, this is the explicit syntax:

[for x in xs do
    match x with
    | Car(_, seats) -> yield seats
    | _ -> ()
] |> List.sum
Laurent
  • 2,951
  • 16
  • 19
  • Interesting. Do you have a reference/link that describes the "cleaning up" you mentioned, and discuss the justification of this change? – gasche Apr 12 '11 at 09:00
  • 4
    @gasche: I have old compilers on my computer, I can tell you the change happened between versions 1.9.3.14 and 1.9.6.16. I can't find a proper reference for this, but those release notes mention a syntax simplification: [link](http://blogs.msdn.com/b/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx). There is also a discussion here: [link](http://cs.hubfs.net/forums/thread/12072.aspx). – Laurent Apr 12 '11 at 09:42
  • 1
    As patterns can be complex (or defined as active patterns), it was not always clear for the reader whether a loop was filtering or not. I guess this might be a justification (personally, I enjoyed this syntax). Also, when you see how for loops in computation expressions are desugared, it's clear it's raising a MatchFailureException. – Laurent Apr 12 '11 at 09:53
5

You can silence any warning via the #nowarn directive or --nowarn: compiler option (pass the warning number, here 25 as in FS0025).

But more generally, no, the best thing is to explicitly filter, as in the other answer (e.g. with choose).

Brian
  • 117,631
  • 17
  • 236
  • 300
5

To explicitly state that you want to ignore unmatched cases, you can use List.choose and return None for those unmatched elements. Your codes could be written in a more idomatic way as follows:

let _ = xs |> List.choose (function | Car(kind, _) -> Some kind
                                    | _ -> None)
           |> List.iter (printfn "found %s")

let sum = xs |> List.choose (function | Car(_, seats)-> Some seats
                                      | _ -> None) 
             |> List.sum
pad
  • 41,040
  • 7
  • 92
  • 166