我正在玩一个可扩展记录库,而且我想要编写一个函数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