F# 具有特定类型和属性的枚举

F# 具有特定类型和属性的枚举,f#,pattern-matching,F#,Pattern Matching,我想确认一只动物是否是人,它的名字是亚历克斯 个人对象: type Person(name: string) = member this.Name = name 枚举: type Animal = | Person of Person | Cat | Dog 模式匹配: let KnowAnimal animal = match animal with | Person person && person.Name = "Alex" ->

我想确认一只动物是否是人,它的名字是亚历克斯

个人对象:

type Person(name: string) =
        member this.Name = name
枚举:

type Animal =
| Person of Person
| Cat
| Dog
模式匹配:

let KnowAnimal animal =
    match animal with
    | Person person && person.Name = "Alex" -> 1
    | Cat -> 2
    | Dog -> 3
    | _ -> 4

您可以在以下情况下使用
指定图案保护:

let KnowAnimal animal =
    match animal with
    | Person person when person.Name = "Alex" -> 1
    | Cat -> 2
    | Dog -> 3
    | _ -> 4