【发布时间】:2019-06-06 12:57:38
【问题描述】:
来自 F# tour 我有这个例子
type Person = {
First : string
Last : string
}
/// A Discriminated Union of 3 different kinds of employees
type Employee =
| Engineer of engineer: Person
| Manager of manager: Person * reports: List<Employee>
| Executive of executive: Person * reports: List<Employee> * assistant: Employee
let rec findDaveWithOpenPosition(emps: List<Employee>) =
emps
|> List.filter(function
| Manager({First = "Dave"}, []) -> true
| Executive({First = "Dave"}, [], _) -> true
| _ -> false
)
但是我想在匹配对象后访问对象,如下所示:
let rec findDaveWithOpenPos2(emps: List<Employee>) =
List.filter (fun (e:Employee) ->
match e with
| Manager({First = "Dave"}, []) -> e.Last.Contains("X") //Does not compile
| Executive({First = "Dave"}, [], _) -> true
| _ -> false
) emps
所以我想在右侧静态键入“e”作为 Person 或 Employee 或 Manager 变量,并可以访问它的属性。 可能吗?有没有更好的结构?
【问题讨论】:
标签: f# pattern-matching