Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 判别联合内的匿名记录类型_F#_Algebraic Data Types - Fatal编程技术网

F# 判别联合内的匿名记录类型

F# 判别联合内的匿名记录类型,f#,algebraic-data-types,F#,Algebraic Data Types,F#教程包括以下片段: /// A record for a person's first and last name type Person = { First : string Last : string } /// define a discriminated union of 3 different kinds of employees type Employee = | Engineer of Person | Manager of

F#教程包括以下片段:

/// A record for a person's first and last name
type Person = {     
    First : string
    Last  : string
}

/// define a discriminated union of 3 different kinds of employees
type Employee = 
    | Engineer  of Person
    | Manager   of Person * list<Employee>            // manager has list of reports
    | Executive of Person * list<Employee> * Employee // executive also has an assistant
遗憾的是,管理者和执行者的定义现在犯了一个错误:“这个结构被弃用;考虑使用单独的记录类型。”好吧,看起来公平,我们称之为ManagerType。但是等等。。。ManagerType指的是Employee(对于报告部分),Employee指的是ManagerType(对于Manager选项)

这里有解决办法吗?两个数据结构不能相互定义吗?

如果您使用的是F#v3.1,您可以使用(并保护您的敏感度):


相互递归的类型是用
type…=。。。而且

type Employee = 
    | Engineer  of Person
    | Manager   of Manager 
    | Executive of Executive
and Manager = { Name: Person; Reports: Employee list }
and Executive = { Name: Person; Reports: Employee list; Assistant: Employee}

您可以使用
定义相互依赖的类型:

    type Employee = 
    | Engineer  of Person
    | Manager   of Manager            // manager has list of reports
    | Executive of Executive
    and Manager = { Name: Person; Reports: Employee list }
    and Executive = { Name: Person; Reports: Employee list; Assistant: Employee }

从FSharp 4.6开始,您可以与联合体一起使用。因此,您的示例可以这样定义:

type Person = {     
    First : string
    Last  : string
}

type Employee =
    | Engineer of Person
    | Manager of {| Name: Person; Reports: Employee list |}
    | Executive of {| Name: Person; Reports: Employee list; Assistant: Employee |}

很抱歉Lee悄悄地加快了速度:)这个解决方案不错(它确实避免了我虚弱的体质),但是如果我只想从经理那里提取报告,那么在模式匹配表达式中会是什么样子呢?使用其他解决方案,它将是:
|Manager({Reports=rep})->无论什么
。我没有安装v3.1,但根据我的答案中链接的MSDN页面,它将是
|Manager(Reports=rep)->……
。它看起来像这个经理(人,报告)。唯一的区别是你可以给他们命名。我们也可以使用继承吗?管理者能从经理那里得到什么?或者这只适用于记录?为了完整性:匿名记录不能进行模式匹配()。
    type Employee = 
    | Engineer  of Person
    | Manager   of Manager            // manager has list of reports
    | Executive of Executive
    and Manager = { Name: Person; Reports: Employee list }
    and Executive = { Name: Person; Reports: Employee list; Assistant: Employee }
type Person = {     
    First : string
    Last  : string
}

type Employee =
    | Engineer of Person
    | Manager of {| Name: Person; Reports: Employee list |}
    | Executive of {| Name: Person; Reports: Employee list; Assistant: Employee |}