已定义 class 的可区分联合
Discriminated union with an already defined class
我有学校 class(有 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"中的错误是第二种情况:"The constructor is applied to 0 arguments, but expect 1".
应该是
let knowType building =
match building with
| House -> "A house!"
| School _ -> "A school"
您需要为 of School
部分提供一个变量。 _
就是忽略
我有学校 class(有 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"中的错误是第二种情况:"The constructor is applied to 0 arguments, but expect 1".
应该是
let knowType building =
match building with
| House -> "A house!"
| School _ -> "A school"
您需要为 of School
部分提供一个变量。 _
就是忽略