Joh*_*Doe 1 f# discriminated-union
我有School类(有2个构造函数):
type School(name, antiquity) =
member this.Name: string = name
member this.Antiquity: int = antiquity
new(name) = School(name, 0)
Run Code Online (Sandbox Code Playgroud)
和建筑类型:
type Building =
| House
| School of School
Run Code Online (Sandbox Code Playgroud)
我想知道具有"knowType"功能的建筑物的类型:
let knowType building =
match building with
| House -> "A house!"
| School -> "A school" // Error
Run Code Online (Sandbox Code Playgroud)
"knowType"中的错误在第二种情况下:"构造函数应用于0参数,但期望为1".
它应该是
let knowType building =
match building with
| House -> "A house!"
| School _ -> "A school"
Run Code Online (Sandbox Code Playgroud)
您需要为of School零件提供变量. _只是意味着它被忽略了