爲什麼不類型檢查?爲什麼沒有這個函數簽名類型檢查?
data MyEither a b = MyLeft a | MyRight b
deriving (Read, Show)
extractEither :: MyEither a b -> c
extractEither (MyLeft p) = p
編譯器顯示:
Couldn't match type `a' with `c'
`a' is a rigid type variable bound by
the type signature for extractEither :: MyEither a b -> c
at /Users/tongmuchenxuan/playground/test.hs:5:1
`c' is a rigid type variable bound by
the type signature for extractEither :: MyEither a b -> c
at /Users/tongmuchenxuan/playground/test.hs:5:1
In the expression: p
In an equation for `extractEither': extractEither (MyLeft p) = p
是不是'C」一般足以捕捉任何類型的?
謝謝。你們兩個的答案都很清楚,解決了我的難題! – 2012-07-17 08:17:45
@ M.Tong:不要忘記將其中的一個標記爲接受的答案。 – 2012-07-17 08:28:34
其實這個問題並不是以動態類型爲前提的,而只是子類型和upcasts,可以靜態檢查哪個AFAIK。雖然Haskell沒有這些。 – 2012-07-17 17:10:37