下面的代码无法编译:
假设 MyType 不是一个单子类型,我该如何获取一个 Lens(或 Traversal,或者任何最适合的东西 - 我不确定区别)来访问这个嵌套字段?最好能够读取和更新。
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data MyType = MyType Int
data Outer = Outer { _inners :: [ Inner ] }
data Inner = Inner { _val :: MyType }
$(makeLenses ''Outer)
$(makeLenses ''Inner)
i1 = Inner (MyType 1)
i2 = Inner (MyType 2)
o = Outer [i1, i2]
x = o ^. inners . ix 0 . val
出现了此错误
Toy.hs:17:23:
No instance for (Data.Monoid.Monoid MyType)
arising from a use of `ix'
Possible fix:
add an instance declaration for (Data.Monoid.Monoid MyType)
In the first argument of `(.)', namely `ix 0'
In the second argument of `(.)', namely `ix 0 . val'
In the second argument of `(^.)', namely `inners . ix 0 . val'
假设 MyType 不是一个单子类型,我该如何获取一个 Lens(或 Traversal,或者任何最适合的东西 - 我不确定区别)来访问这个嵌套字段?最好能够读取和更新。