I'm supplying a function to idleCallback
This notation works:
idleCallback $= Just (do
modifyIORef world play
postRedisplay Nothing)
Why doesn't this (seemingly similar) notation work?
idleCallback $= Just $ do
modifyIORef world play
postRedisplay Nothing
To save your hoogling, the types are:
($=) :: HasSetter s => s a -> a -> IO ()
type IdleCallback = IO ()
data SettableStateVar a
idleCallback :: SettableStateVar (Maybe IdleCallback)
postRedisplay :: Maybe Window -> IO ()
modifyIORef :: IORef a -> (a -> a) -> IO ()
GHC says:
Couldn't match expected type `Maybe IdleCallback'
with actual type `a0 -> Maybe a0'
In the second argument of `($=)', namely `Just'
In the expression: idleCallback $= Just
In a stmt of a 'do' block:
idleCallback $= Just
$ do { modifyIORef world play;
postRedisplay Nothing }
Can this be written without wrapping the do
block in parenthesis?
Basically, as we discussed, this do notation is used to handle the input and output operation in Haskell; we also have some more ways which provide us to handle these operations, but out of which do notation is very easy to use and implement in Haskell.
The bind operations, >> and >>=, combine two monadic values while the return operation injects a value into the monad (container).
In general terms, where f and g are functions, (f . g) x means the same as f (g x). In other words, the period is used to take the result from the function on the right, feed it as a parameter to the function on the left, and return a new function that represents this computation."
In Haskell, the colon operator is used to create lists (we'll talk more about this soon). This right-hand side says that the value of makeList is the element 1 stuck on to the beginning of the value of makeList .
It is a precedence error.... ($=
) is binding more tightly than ($
). You can see this in the error message:
Couldn't match expected type `Maybe IdleCallback'
with actual type `a0 -> Maybe a0'
In the second argument of `($=)', namely `Just'
It thinks that the second argument of ($=
) is simply Just
(which is a valid Haskell type referencing a function). If you put parenthesis around the whole Just
, including the do
-block, it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With