我有没有办法只从具有镜头的模块中导出特定的吸气剂xor setter?
例如,让我们假设一个数据结构具有始终为>= 0
的不变量,仅通过递增并仅使用初始值0
创建来修改:
module Something
( Counter
-- export only `count` getter
, make
, increment
) where
data Counter = Counter { _count :: Int } deriving (Eq)
makeLenses ''Positive
make :: Counter
make = Counter 0
increment :: Counter -> Counter
increment c = c ^. count %~ (+1)
我怎样才能导出count
吸气剂?
答案 0 :(得分:10)
data Counter = Counter { _count' :: Int } deriving (Eq)
makeLenses ''Counter
count :: Getter Counter Int
count = count'
答案 1 :(得分:2)
如果您只想生成Getter
和Fold
光学器件(视情况而定),可以使用新的generateUpdateableOptics
设置
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Counter = Counter { _count :: Int } deriving (Eq)
let rules = set generateUpdateableOptics False lensRules in
makeLensesWith rules ''Counter
-- Generates:
-- count :: Getter Counter Int