Can't get (->) r monad to work with SDL2 rendering

I have a small working SDL2 sample (a simple port of a C ++ tutorial) and I'm trying to refactor my rendering code to use the (->) r monad. Here's a working part of the sample without refactoring

renderClear renderer
renderCopy renderer texture nullPtr nullPtr
renderPresent renderer

      

Make and run - there is a texture. Both

renderClear renderer
flip ($) renderer $ do
    copy texture nullPtr nullPtr
renderPresent renderer

      

and

renderClear renderer
renderCopy renderer texture nullPtr nullPtr
flip ($) renderer $ do
    present

      

works great. Nevertheless,

renderClear renderer
flip ($) renderer $ do
    copy texture nullPtr nullPtr
    present

      

just draws a black window and it is. present

is an alias renderPresent

, and copy

- renderCopy

with the first argument wrapped to the end. Does it have something to do with these IO-enabled functions? How can I fix this?

+3


source to share


1 answer


When you combine it into

flip ($) renderer $ do
    copy texture nullPtr nullPtr
    present

      

the whole block do

is in the monad (->) r

and therefore the actions are not sequenced with IO

or any other basic monad, but instead are used essentially

(f >> g) x = g x

      



leaving only present

in combination.

The way to fix this, I think, is to use a monad transformer ReaderT

that knows how to use the base monad when sequencing, turning it into something like (you need to override copy

and present

accordingly)

flip runReaderT renderer $ do
    copy texture nullPtr nullPtr
    present

      

+5


source







All Articles