Given the following:
type IFruit = interface end
type Avocado = { color : string; age : int } interface IFruit
let (|AvocadoTexture|) (a : Avocado) = if a.age < 7 then "firm" else "mushy"
... Why does this work:
let texture (f : IFruit) =
match f with
| :? Avocado as a -> if a.age < 7 then "firm" else "mushy"
| _ -> String.Empty
... but not this?
let texture (fruit : IFruit) =
match fruit with
| AvocadoTexture t -> t // "The type IFruit does not match the type Avocado"
| _ -> String.Empty
fruit
may be anyIFruit
, but theAvocadoTexture
Active Pattern only accepts the specific implementationAvocado
, as per the type annotation ofa
. If you want the Active Pattern to accept anyIFruit
, but only return a useful value for anAvocado
, you can make it partial:Now your
texture
function works as you wanted: