简体   繁体   中英

Returning derived classes in F#

Consider the following code, which declares an abstract base class called shape and another class called square which inherits from shape :

open System

[<AbstractClass>]
type shape() =
    abstract area : int

type square(side) =
    inherit shape()
    override this.area = side * side

let return_square(n): shape =
    new square(n)

[<EntryPoint>]
let main argv =
    let foo = new square (3)
    printfn "%d" foo.area

    let bar = return_square(4)
    printfn "%d" bar.area
    0 // Return 0 to OS

The creation of foo works fine, but when I attempt to return a shape from return_square I receive the following error message:

This expression was expected to have type
    'shape'    
but here has type
    'square'

Can anyone shed some light as to what I'm doing wrong? Shouldn't I be able to return a square since it's a type of shape ?

Many TIA

The fsharp compiler will not automatically do the cast from square to shape for you.

You can solve this for example by changing your code to:

let return_square(n): shape =
    new square(n) :> shape

or you don't even have to specify the type:

let return_square(n): shape =
    new square(n) :> _

an other alternative is to omit the type from the type signature and only do the cast:

let return_square(n) =
    new square(n) :> shape

Also, the parenthesis and new keyword is not really needed when calling the constructor so you could also simplify it to just:

let return_square n =
    square n :> shape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM