f# - How do I Pattern Match on the actual container of Discriminated Union cases? -


out of curiosity, how pattern match on actual container of discriminated union cases?

specifically, how can know if value of specific type?

i tried using pattern construct (i.e. :?):

let isvehicle = fun -> match                          | :? vehicle -> "is vehicle"                          | _          -> "is not vehicle" 

error:

this runtime coercion or type test type 'a vehicle involves indeterminate type based on information prior program point. runtime type tests not allowed on types. further type annotations needed.

here's entire code:

type vehicle =      | car     | tank     | helicopter  let move = function     | car        -> "wheels spin"     | tank       -> "tracks roll"     | helicopter -> "blades spin"  let isvehicle = fun -> match                          | :? vehicle -> "is vehicle"                          | _          -> "is not vehicle" 

you should add type annotation lambda parameter:

let isvehicle = fun (a:obj) -> match                                | :? vehicle -> "is vehicle"                                | _          -> "is not vehicle" 

Comments