为什么不类型检查?为什么没有这个函数签名类型检查?
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