Match on discriminated union case not contents

Viewed 526

Is it possible in F# to match a Discriminated Union based on its case rather than by case contents? For example, if I wanted to filter a list by elements that are of the case Flag, is it possible to filter as such? Currently, I am forced to have three separate functions to filter the way I desire. This is the approach I have so far:

type Option = 
 {Id : string
  Arg : string}

type Argument =
     | Flag of string
     | Option of Option
     | Unannotated of string

//This is what I'm going for, but it does not work as the "other" match case will never be matched
let LocateByCase (case:Argument) (args : Argument List) =
    args
    |> List.filter (fun x -> match x with
                             | case -> true
                             | _ -> false)

let LocateAllFlags args =
    args
    |> List.filter (fun x -> match x with
                             | Flag y -> true
                             | _ -> false)
let LocateAllOptions args =
    args
    |> List.filter (fun x -> match x with
                             | Option y -> true
                             | _ -> false)

let LocateAllUnannotated args =
    args
    |> List.filter (fun x -> match x with
                             | Unannotated y -> true
                             | _ -> false)

Am I missing some facet of the F# language that would make this much easier to deal with?

1 Answers
Related