我会在有状态计算中使用与 setter 和 getter 相同的镜头。而且 GHC 似乎无法推断出 Functor f 的共同类型。
import Lens.Family
import Lens.Family.State
import Control.Monad.State
-- | Run computation inside modified state
with :: Functor f => LensLike' f s a -> a -> State s b -> State s b
with lens value comp = do
value' <- use lens
lens .= value
res <- comp
lens .= value'
return res
所以我的问题是,是否有可能实现这种行为,或者我应该为 setter 和 getter 使用单独的镜头?谢谢!