2

What is the difference (if any) between these two F# type signatures?

UseTheStream<'a when 'a :> Stream> : 'a -> unit

and

UseTheStream : (stream : Stream) -> unit

Do they mean the same thing in this case?

msdn says the following about the (:>) Type Constraint

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.

This would indicate that the two signatures are saying the same thing. So Functionally, how are they different?

4

1 回答 1

15

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

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

let s1 = new MemoryStream() |> UseTheStream
let s2 = new MemoryStream() |> UseTheStreamStrict

s1MemoryStreams2Stream

注意:#T'U when 'U :> T.

于 2012-09-28T14:51:08.540 回答