我有这个简单的代码
class Hashable a where
hash :: Bits b => a -> b
instance Hashable Int where
hash = id
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误
Could not deduce (b ~ Int)
from the context (Bits b)
bound by the type signature for hash :: Bits b => Int -> b
at Memo.hs:11:5-8
`b' is a rigid type variable bound by
the type signature for hash :: Bits b => Int -> b at Memo.hs:11:5
Expected type: Int -> b
Actual type: Int -> Int
Relevant bindings include hash :: Int -> b (bound at Memo.hs:11:5)
In the expression: id :: Int -> Int
In an equation for `hash': hash = id :: Int -> Int
In the instance declaration for `Hashable Int'
Run Code Online (Sandbox Code Playgroud)
我真的不明白.该类仅指定您可以从类型为a的对象获取Bits实例.
存在Bits Int,为什么不适hash = id用于Int?
(Bits b=>a->b)意味着您可以提取任何类型的Bits实例,而不仅仅是一个. hash = id只适用于类型Int,编译器基本上抱怨"嘿,所有其他Bits实例怎么样?"
| 归档时间: |
|
| 查看次数: |
42 次 |
| 最近记录: |