Skip to content

Restore FlatMap.map2Eval laziness#4859

Open
satorg wants to merge 1 commit intotypelevel:mainfrom
satorg:fix-map2eval
Open

Restore FlatMap.map2Eval laziness#4859
satorg wants to merge 1 commit intotypelevel:mainfrom
satorg:fix-map2eval

Conversation

@satorg
Copy link
Copy Markdown
Contributor

@satorg satorg commented May 6, 2026

Resolves #4858.

@johnynek
Copy link
Copy Markdown
Contributor

johnynek commented May 6, 2026

I guess the motivation was that inside the flatMap the .value might not blow the stack and if the F[_] is lazy it would possibly be safe.

The change makes us always evaluate the Eval[F[B]], but it is safe.

The motivation of the map2Eval is for Monads like Try/Either that a failure on the left avoids evaluating the right. This change will evaluate the Eval in those cases and no longer short circuit.

Maybe to mitigate this we want to make sure any shortcircuiting Monads in cats override map2Eval so we don't regress on cases like Try, Future, Either. Maybe others.

A middle ground could be to override StackSafeMonad to use the .value on the idea that the flatMap is always lazy in a StackSafeMonad. That may work or may have other issues that I don't see.

@satorg
Copy link
Copy Markdown
Contributor Author

satorg commented May 6, 2026

Maybe to mitigate this we want to make sure any shortcircuiting Monads in cats override map2Eval so we don't regress on cases like Try, Future, Either. Maybe others.

Yes, good call. I think that would a correct direction to go. We don't know and can't guess whether current FlatMap instance is for a short-circuiting Monad or not. The goal for generic methods is to be as safe as possible (I believe so). The concrete implementation though does know whether their Monad can short-circuit or not. So, it is more likely their responsibility to optimize for short-circuiting cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FlatMap.map2Eval is not lazy and blows the stack in cases where it probably shouldn't

2 participants