我正在玩一個可擴展記錄庫,而且我想要編寫一個函數field
,根據Symbol
密鑰是否在密鑰列表中,它可以作爲Lens
或Traversal
運行。該類型的家庭給出:類型系列不能返回RankN類型 - 解決方法或替代方法?
type family LensOrTraversal key keys s t a b where
LensOrTraversal key '[] s t a b =
Traversal s t a b
LensOrTraversal key (key =: val ': xs) s t a b =
Lens s t a b
LensOrTraversal key (foo =: bar ': xs) s t a b =
LensOrTraversal key xs s t a b
此代碼給我一個錯誤:
/home/matt/Projects/hash-rekt/src/Data/HashRecord/Internal.hs:433:5:
error:
• Illegal polymorphic type: Traversal s t a b
• In the equations for closed type family ‘LensOrTraversal’
In the type family declaration for ‘LensOrTraversal’
理想情況下,我希望能夠爲這兩個鏡頭和遍歷重用field
名字,因爲它會允許你寫
>>> let testMap = empty & field @"foo" .~ 'a'
>>> :t testMap
HashRecord '["foo" =: Char]
>>> testMap ^. field @"foo"
'a'
>>> testMap ^. field @"bar"
Type error
>>> testMap ^? field @"bar"
Nothing
它遵循共同的lens
成語。我可以提供一個我想要的fieldTraversal
函數,但如果可能的話,我寧願超載名稱field
。你將如何解決這種類型家庭的限制?
[使用RankNTypes和TypeFamilies的非法多態或限定類型]可能重複(http://stackoverflow.com/questions/13846284/illegal-polymorphic-or-qualified-type-using-rankntypes-and-typefamilies) –
@ AntalSpector-Zabusky也許是相關的,但這絕對不是重複的。 – leftaroundabout