参数多态性与亚型多态性F#

Eri*_*ric 2 inheritance f# functional-programming subtyping parametric-polymorphism

这两个F#类型签名之间有什么区别(如果有的话)?

UseTheStream<'a when 'a :> Stream> : 'a -> unit
Run Code Online (Sandbox Code Playgroud)

UseTheStream : (stream : Stream) -> unit
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它们的意思是一样的吗?

msdn说出以下关于(:>)类型约束的内容

type-parameter :> type --   The provided type must be equal to or derived from the type      specified, or, if the type is an interface, the provided type must implement the interface.
Run Code Online (Sandbox Code Playgroud)

这表明两个签名正在说同样的话.从功能上来说,它们有何不同?

Dan*_*iel 15

它们是不同的.最重要的是,第一个功能是通用的.在您的示例中,它可能无关紧要,但如果type参数影响函数的返回类型,它会:

let UseTheStream (stream: #Stream) = stream
let UseTheStreamStrict (stream: Stream) = stream

let s1 = new MemoryStream() |> UseTheStream
let s2 = new MemoryStream() |> UseTheStreamStrict
Run Code Online (Sandbox Code Playgroud)

s1MemoryStream.s2Stream.

注意:#T是简写'U when 'U :> T.