Sridhar Ratnakumar
Sridhar Ratnakumar

Reputation: 85272

Error handling on multiple Either values

I have two either values, for example:

Either String Config  -- error string or config parsed
Either String Env     -- error string or environment variables detected

And I'd like to extract their values into this record:

type App = App { config :: Config, env :: Env }

while failing fast if there were errors (the Left value on one of those either values).

I could use two case statements, but I wonder if there is already an abstraction I can use here?

Ideally I would be logging a message upon an error and exit the program immediately.

Upvotes: 1

Views: 322

Answers (1)

gallais
gallais

Reputation: 12103

You can use the fact that Either String is an Applicative for things like this.

Assuming

mcnf :: Either String Config
menv :: Either String Env

You can write

mapp :: Either String App
mapp = App <$> mcnf <*> menv

Upvotes: 6

Related Questions