是否有用于GADT的等价物makeLenses?如果我有一个简单的GADT,比如:
data D a b where
D :: (Ord a, Ord b) => !a -> !b -> D a b有没有一种方法可以通过传入构造函数和字段名列表来自动生成镜头?
发布于 2015-01-26 14:53:57
我不认为它可以自动完成,但在这种情况下,手动编写一些镜头并不是那么困难:
{-# LANGUAGE GADTs #-}
import Control.Lens
data D a b where
D :: (Ord a, Ord b) => !a -> !b -> D a b
field1 :: Lens' (D a b) a
field1 f (D x y) = fmap (\x' -> D x' y) (f x)
field2 :: Lens' (D a b) b
field2 f (D x y) = fmap (\y' -> D x y') (f y)
{- If you want type-changing lenses, you can also use these signatures.
- Note that then the target type Ord constraint has to escape.
field1 :: (Ord a2) => Lens (D a1 b) (D a2 b) a1 a2
field2 :: (Ord b2) => Lens (D a b1) (D a b2) b1 b2
-}似乎有一个有点相关的GitHub issue,其中克梅特声称他们不能为存在量化的领域创造透镜。
https://stackoverflow.com/questions/28145369
复制相似问题