Calling side effect in ReaderT

Hello all,

I’m experimenting with ReaderT, before I successfully ran it with hypertrout, but when I try something simple like print message from env to log (in Aff monad),I get type errors

Could not match type
          
    Record
          
  with type
                            
    Function                
      (ReaderT              
         { message :: String
         }                  
         Aff                
         Unit               
      )                     
                            

while trying to match type { message :: t0
                           | t1           
                           }              
  with type ReaderT               
              { message :: String 
              }                   
              Aff                 
              Unit                
            -> { message :: String
               }                  
               -> Aff Unit        
while checking that expression $1
  has type { message :: t0
           | t1           
           }              
in value declaration main

My code is

module Main where

import Prelude

import Control.Monad.Reader (ReaderT, asks, runReaderT)
import Control.Monad.Reader.Class (class MonadAsk)

import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Class.Console (log)

type Env = 
  { message :: String
  }

type App = ReaderT Env Aff Unit

getMessage :: forall m. MonadAsk Env m => m String
getMessage = asks _.message

runAppM ∷  App -> Env -> Aff Unit
runAppM = runReaderT

run :: App
run = do
  msg <- getMessage
  log msg

main :: Effect Unit
main = launchAff_ $ runAppM run { message = "Hello" }

I also tried to swap run and { message = "Hello" } but then I get

Error found:
in module Main
at src/Main.purs:30:21 - 30:50 (line 30, column 21 - line 30, column 50)

  Could not match type
          
    Record
          
  with type
                            
    Function                
      (ReaderT              
         { message :: String
         }                  
         Aff                
         Unit               
      )                     
                            

while trying to match type { message :: t0
                           | t1           
                           }              
  with type ReaderT               
              { message :: String 
              }                   
              Aff                 
              Unit                
            -> { message :: String
               }                  
               -> Aff Unit        
while checking that expression $1
  has type { message :: t0
           | t1           
           }              
in value declaration main

where t1 is an unknown type
      t0 is an unknown type

thank you in advacne

You should use { message: "Hello" } instead of { message = "Hello" }. The latter is to change the fields of a record and you can’t use it for a record literal. The error is basically telling you that it expects a record before { message = "Hello" } but it got a function instead.

2 Likes