Haskell 自动镜头聚合

Haskell 自动镜头聚合,haskell,haskell-lens,Haskell,Haskell Lens,我刚刚从lens包中发现了makeClass。它允许你做一些类似的事情 data Point2 = Point2 { _x:: Double, _y :: Double } makeClassy ''Point2 data Point3 = Point3 { _p2 : Point2, _z :: Double} makeclassy ''Point3 instance HasPoint2 Point3 where point2 = p2 p3 = Point3 (Point 2

我刚刚从
lens
包中发现了
makeClass
。它允许你做一些类似的事情

data Point2 = Point2 { _x:: Double, _y :: Double }
makeClassy ''Point2

data Point3 = Point3 { _p2 : Point2, _z :: Double}
makeclassy ''Point3

instance HasPoint2 Point3 where
    point2 = p2
p3 = Point3 (Point 2 1 2) 3

p3 ^. x -- 1
p3 ^. y -- 2
p3 ^. z -- 3
然后你可以做类似的事情

data Point2 = Point2 { _x:: Double, _y :: Double }
makeClassy ''Point2

data Point3 = Point3 { _p2 : Point2, _z :: Double}
makeclassy ''Point3

instance HasPoint2 Point3 where
    point2 = p2
p3 = Point3 (Point 2 1 2) 3

p3 ^. x -- 1
p3 ^. y -- 2
p3 ^. z -- 3
这太棒了。但是,是否有一种方法可以在一个步骤中重新组合
makeClassy
实例HasPoint2