【发布时间】:2017-10-01 21:42:21
【问题描述】:
我正在使用一个可扩展的记录库,我想编写一个函数field,它可以根据Symbol 键是否为Lens 或Traversal 来运行在键列表中。类型族给出:
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。您将如何解决类型族的这种限制?
【问题讨论】:
-
@AntalSpector-Zabusky 可能相关,但这绝对不是重复的。
标签: haskell types haskell-lens type-families