Sto usando il codice seguente su un record che ha un campo "_scene" di tipo SceneGraph. Ho creato degli obiettivi usando i makeLenses.Perché il mio tipo di dati necessita di un'istanza di Monoid per utilizzare questo obiettivo?
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
sto ottenendo l'errore:
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
Ma io non capisco perché scenegraph deve essere un'istanza di Monoide al fine di utilizzare questo obiettivo.
Ah! Ecco perché 'makeLenses' creerebbe un prisma. –
Ok, penso di averlo capito. È perché il mio tipo di dati sembra dati Game = GameLoad | Gioco {_scene :: SceneGraph} | GameOver quindi è possibile che 'scene' possa fallire perché il gioco è uno degli altri due costruttori ... – schellsan
Aha. Sì. Devi gestire i casi 'GameLoad' /' GameOver'. – shachaf