我想在有状态计算中使用相同的镜头作为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
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,是否有可能实现这样的行为,还是应该使用单独的镜头作为制定者和吸气剂?谢谢!
我想使用字符串文字作为遍历,但我在类型中有点迷失.是否可以创建此实例?
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import Data.String
import Data.Default
{- Having:
key' :: AsValue t => Text -> Traversal' t (Maybe Value)
_JSON :: (ToJSON a, FromJSON a) => Traversal' t a
-}
instance (AsValue t, FromJSON v, ToJSON v, Default v) => IsString (Traversal' t v) where
fromString k = key' (fromString k) . non (toJSON def) . _JSON
Run Code Online (Sandbox Code Playgroud)
在State monad中实现这样的目标:
"some-key" .= (3 :: Int)
Run Code Online (Sandbox Code Playgroud)
通用量化类型实例的问题.谢谢!