例如使用一個類我有一個代碼以下方案:哈斯克爾:在另一個
class First s where
func1 :: s -> s
class Second a where
func2 :: s -> a s
data D s = D {value :: s}
myFunc2 :: First s => s -> D s
myFunc2 = undefined
在共同FUNC2的說法不能成爲第一的實例。只有當它是First的值實例時,我纔想讓D的第二個實例。 後來我想這種情況下:
instance Second D where
func2 = myFunc2
但我得到一個錯誤:
No instance for (First s)
arising from a use of `myFunc2'
好吧,讓我們的實例是:
instance First s => Second D where
func2 = myFunc2
但是這給了錯誤:
Ambiguous constraint `First s'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '=>'
In the instance declaration for `Second D'
所以,是有一種方法可以從其他類的某些條件中獲得類的實例,但在'=>'之後沒有所有類型變量?
謝謝,這看起來像我能做的最好的事情。 – spontaliku 2012-03-15 12:54:40