SceneGraphタイプのフィールド '_scene'を持つレコードで、以下のコードを使用しています。私はmakeLensesを使ってレンズを作りました。このレンズを使用するには、私のデータ型がMonoidのインスタンスに必要なのはなぜですか?
inputGame :: Input -> Game -> Game
inputGame i g = flip execState g $ do
let es = g ^. userInput . events
sg = g ^. scene
userInput .= i
scene .= foldl (flip inputEvent) sg es
inputEvent :: InputEvent -> SceneGraph -> SceneGraph
inputEvent (WindowSizeChangedTo (w,h)) (SceneRoot _ _ sg) = SceneRoot w h sg
inputEvent _ sg = sg
私はエラーになっています:
No instance for (Monoid SceneGraph) arising from a use of `scene'
Possible fix: add an instance declaration for (Monoid SceneGraph)
In the second argument of `(^.)', namely `scene'
In the expression: g ^. scene
In an equation for `sg': sg = g ^. scene
をしかし、シーングラフは、このレンズを使用するために、モノイドのインスタンスである必要があり、なぜ私は理解していません。
ああ!だから、 'makeLenses'がプリズムを作り出す理由です。 –
さて、私はそれを得ると思います。私のデータ型がデータのように見えるからです。Game = GameLoad |ゲーム{_scene :: SceneGraph} | GameOverので、ゲームが他の2つのコンストラクタの1つであるために 'scene'が失敗する可能性があります。 – schellsan
Aha。はい。あなたは 'GameLoad' /' GameOver'のケースを扱う必要があります。 – shachaf