Why upcast is not necessary in this let bound function?

♀尐吖头ヾ 提交于 2019-12-14 03:12:16

问题


According to MSDN

Upcasting is applied automatically when you pass arguments to methods on an object type. However, for let-bound functions in a module, upcasting is not automatic, unless the parameter type is declared as a flexible type.

But

type C() =
    member this.T() = ()
type D() =
    inherit C()

let myfun (x: C)=
    ()

let d = D()

myfun d

I don't need to upcast at all.


回答1:


Have a look at the F# spec where it says:

This means that F# functions whose inferred type includes an unsealed type in argument position may be passed subtypes when called, without the need for explicit upcasts. For example:

(The example showed there is almost the same as yours)

Also note that for let bound functions upcasting is automatically except in some cases, for example when the argument is composed, if your function was:

let myfun (x: C option)=
    ()

upcast is no longer automatically.



来源:https://stackoverflow.com/questions/26791480/why-upcast-is-not-necessary-in-this-let-bound-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!