r/purescript • • Oct 06 '17

function vs case pattern

I'm learning Purescript and Halogen and am running into confusion over the use of a single case expression vs a function with multiple patterns. In a Halogen example we have...

  eval :: Query ~> H.ComponentDSL State Query Message m
  eval = case _ of
    Toggle next -> do
      state <- H.get
      let nextState = not state
      H.put nextState
      H.raise $ Toggled nextState
      pure next
    IsOn reply -> do
      state <- H.get
      pure (reply state)

I refactored this to what I thought was exactly equivalent, but it fails to compile:

  eval :: Query ~> H.ComponentDSL State Query Message m
  eval Toggle next = do
      state <- H.get
      let nextState = not state
      H.put nextState
      H.raise $ Toggled nextState
      pure next
  eval IsOn reply = do
      state <- H.get
      pure (reply state)

Compile error:

Error found:
in module Button
at src/Button.purs line 54, column 3 - line 54, column 56

  Could not match type
                                                    
    HalogenM Boolean Query (Const Void) Void Message
                                                    
  with type
            
    Function
            

while checking that expression \$3 ->                        
                                 case $2 $3 of               
                                   Toggle next -> (...) (...)
                                   IsOn reply -> (...) (...) 
  has type HalogenM Boolean Query (Const Void) Void Message m0 a1
in value declaration myButton

where m0 is a rigid type variable
        bound at line 20, column 3 - line 63, column 24
      a1 is a rigid type variable

See https://github.com/purescript/documentation/blob/master/errors/TypesDoNotUnify.md for more information,
or to contribute content related to this error.

I'm stumped as to why these might be different. Any suggestions?

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/goertzenator Oct 06 '17

Thanks for both your answers. I'm relieved it is just parenthesis and not some deep type issue. :)