ReaderT pattern reading env variable

Hello all,

I’m experimenting with ReaderT pattern and I defined env like

type Env = 
  { message :: String
  }

Then I defined application monad like

type AppM m = ReaderT Env m

Example is written in hipertrout and there this is action I’m triing to execute (which should return message)

data Greeting = Greeting String

This is handler which read message from Env and return it in action

greetingResource :: {"GET" :: ExceptT RoutingError (AppM Aff) Greeting}
greetingResource = 
  let message = do
                  env <- ask
                  pure env.message
  in {"GET": Greeting <$> message}

I’m triing to extract retrieving message to separate function, but I failed to figure out the types

getMessage :: ?
getMessage = do
   env <- ask
   pure env.message

Thank you in advance :slight_smile:

getMessage :: forall m. MonadAsk Env m => m String

Generally, you write the program logic using type class constraints and then “implement” that logic using concrete monad transformers (e.g. ReaderT).

2 Likes

Thank you, I was little byt blinded by triing to put there AppM somehow, but MonadAsk is enought, I’m just learning purescript and struggle to get things work, hopefully I get to point that I will struggle to make things pretty :smiley:

Also note that for reading and transforming the value at the same time there is asks:

getMessage = asks _.message
3 Likes