2017-09-25 45 views
1

我有學校類(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 

而且我想知道是什麼類型的建築物與功能「知道類型」:

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

「knowType」中的錯誤是在第二種情況下:「該構造函數被應用0參數,但期望1「。

回答

6

應該

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

你需要給一個變量爲of School一部分。 _只是意味着它被忽略了