34

What is the relationship between the Purescript types Eff and Aff? Is it possible to convert between them?

I'm just starting out with Purescript coming from Haskell, and it seems like both types roughly serve the role that IO has in Haskell. Is that fair to say?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137

1 Answers1

53

Eff is a synchronous effect monad. It is used to sequence effectful foreign JavaScript code - things like random number generation, reading and writing mutable values, writing to the console and throwing and catching exceptions.

Aff is an asynchronous effect monad. It can handle and sequence effectful asynchronous code, like AJAX requests, timeouts, and network and file IO. It can also perform synchronous effects by using liftEff. And it also provides a nice mechanism for handling errors.

It is possible to convert from Eff to Aff using liftEff (everything which doesn't pause is an instance of something which is allowed to pause), but the other direction is not possible in general. Aff actions can be run in an Eff context by providing a callback.

Haskell's IO is similar to both, but closer to Aff than Eff in that IO actions can represent asynchronous things (see threadDelay for example).

Phil Freeman
  • 4,199
  • 1
  • 20
  • 15
  • 7
    I think it is worth mentioning `launchAff` which can be used to run an `Aff` computation synchronously if need be. It is defined in `Control.Monad.Aff`. Pursuit will have a better description of what it does and how it works than I can fit in a comment. – Nick Brady May 10 '17 at 21:46