与已定义的类相区别的联合

时间:2017-09-25 11:22:23

标签: f# discriminated-union

我有学校课程(有2个构造函数):

type School(name, antiquity) = 
    member this.Name: string = name
    member this.Antiquity: int = antiquity

    new(name) = School(name, 0)

建筑类型:

type Building =
| House
| School of School

我想知道具有“knowType”功能的建筑物的类型:

let knowType building =
    match building with
    | House -> "A house!"
    | School -> "A school" // Error

“knowType”中的错误在第二种情况下:“构造函数应用于0个参数,但期望为1”。

1 个答案:

答案 0 :(得分:6)

应该是

let knowType building =
    match building with
    | House -> "A house!"
    | School _ -> "A school" 

您需要为of School部分提供变量。 _只是意味着它被忽略