5
的代碼少部分的突出問題:柯里在F#「靈活的類型」
open System.IO
let do_smth i (stm : #System.IO.Stream) = // val do_smth : 'a -> #Stream -> unit
(*....*)
()
type SomeOps = SomeOps with
static member op (i : int) = do_smth i
let test_currying i = do_smth i // val test_currying : 'a -> (Stream -> unit)
// NB: Stream, not #Stream
let main() =
use stm = new System.IO.MemoryStream()
test_currying 42 stm // OK, upcasted to Stream somehow
SomeOps.op 42 stm // compiler error!
有人能解釋一下,爲什麼是編譯器的行爲在最後兩行如此不同?爲什麼我們在test_currying
函數中丟失了信息(關於靈活#Stream
)?
所以,這是正確的行爲,對吧? – qehgt 2012-03-19 19:05:48
我不認爲編譯器目前所做的是正確的行爲。我沒有看到兩個案件行爲不同的理由。這就是爲什麼我說它看起來像一個bug。 – 2012-03-20 01:08:05
好的,我明白了。謝謝。 – qehgt 2012-03-20 03:40:37