As the title says, I am trying to use logShow inside of my handleAction function.
I imported the Effect.Console (logShow)
and tried to use it like this, everytime a button is clicked:
handleAction ∷ forall o m. Action → H.HalogenM State Action () o m Unit
handleAction = case _ of
Update -> do
logShow "Hello"
H.modify_ \st -> st { field3 = st.field3 + 1 }
But I only get the following Error, and I don't understand very much, as I am very new to purescript and functional programming at all.
Could not match type
Effect
with type
HalogenM
{ field1 :: Int
, field2 :: Int
, field3 :: Int
}
Action
()
o0
m1
while trying to match type Effect t2
with type HalogenM
{ field1 :: Int
, field2 :: Int
, field3 :: Int
}
Action
()
o0
m1
Unit
while checking that expression (discard (logShow "Hello")) (\$__unused -> modify_ (\st -> ... ))
has type HalogenM
{ field1 :: Int
, field2 :: Int
, field3 :: Int
}
Action
()
o0
m1
Unit
in value declaration handleAction
where m1 is a rigid type variable
bound at (line 77, column 16 - line 80, column 51)
o0 is a rigid type variable
bound at (line 77, column 16 - line 80, column 51)
t2 is an unknown type
PureScript(TypesDoNotUnify)
I am glad about any clue.
I'm not an expert in PS but I'll try my best to answer it. So please correct me if I'm wrong:
logShowtype signature isMonadEffect m => Show a => a -> m Unit, means that the caller of this function should have an instance of or be constrained byMonadEffect.In your case,
handleActionhas a type signature offorall o m. Action → H.HalogenM State Action () o m Unitand you calllogShowinside it. Yourmhere doesn't describe any particular monad while as we knew already, the caller oflogShowshould be constrained byMonadEffect.There are a couple of ways to solve this:
You can add
MonadEffectconstraint to yourhandleActiontype signature like soThis should work since
HalogenMhas instance ofMonadEffectas long as yourmalso hasMonadEffectinstance. See hereChange your
mtoAffasAffhas instance ofMonadEffect. IIRC, Halogen requires yourmto be or has an instance ofAffAffas you can see in Thomas' great repository hereEDIT: The explanation above assumes you import
logShowfromEffect.Class.ConsoleBut if you're using
logShowfromEffect.Consolewhich has type signatureShow a => a -> Effect Unit, then we need some function that converts theEffectmonad to yourm(this function should have type sigEffect a -> m a). AndliftEffectis exactly what you're looking for.Hope this helps :)