Search code examples
f#discriminated-union

Discriminated union with an already defined class


I have the School class (with 2 constructors):

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

    new(name) = School(name, 0)

And the types of building:

type Building =
| House
| School of School

And I want know what type is a building with the function "knowType":

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

The error in "knowType" is in the second case: "The constructor is applied to 0 arguments, but expect 1".


Solution

  • It should be

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

    You need to give a variable for the of School part. _ just means it is ignored