我在使用下面的代码处理一个包含类型为SceneGraph的'_scene'字段的记录。我已经使用makeLenses创建了它的镜头。
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
但我不明白为什么SceneGraph必须是Monoid的实例才能使用这个透镜。
makeLenses
会创建一个棱镜的原因。 - J. Abrahamsonscene
可能会失败... - schellsanGameLoad
/GameOver
这两种情况。 - shachafGame
只有一个参数(但我认为在这里生成棱镜是有点不直观的行为)。 - shachaf