Michael
Michael

Reputation: 42050

Why do we need Monad Transformers in Scala?

My understanding is the following:

Suppose M1 and M2 are monads, i.e. they provide functions unit and flatMap that comply to the monadic laws. Unfortunately we cannot create unit and flatMap for M1[M2] . It looks like the unit and flatMap don't always exist for any M1 and M2. I do not know if we need to prove it.

So the solution is to create a wrapper W for M1[M2], so that W[M1, M2] is a monad, and use it instead. This wrapper W is called Monad Transformer.

Do I understand it correctly ?

Upvotes: 8

Views: 466

Answers (1)

Yuriy
Yuriy

Reputation: 2769

You are right. Typically they are used in for-comprehension constructions where you need to combine monads properties.

Monad transformers "step-by-step" here.

Upvotes: 3

Related Questions