play-monadic-actions
play-monadic-actions copied to clipboard
inference with overloaded `?| ` operator
I've found that the overloading of ?|
breaks type inference for pattern matching anonymous functions.
I use the following service error pattern heavily, but this doesn't compile:
sealed trait Error
case object IdNotFound extends Error
def service(id: Int): EitherT[Future, Error, String]
for {
name <- service(1) ?| { case IdNotFound => NotFound } // can't infer the function, not thunk, variant of ?|
} yield Ok(name)
In my own fork of the lib, I've also defined a non-overloaded ?||
operator on StepOps
def ?||(failureHandler: B => Result): Step[A] = ?|(failureHandler)
which allows this to compile:
for {
name <- service(1) ?|| { case IdNotFound => NotFound }
} yield Ok(name)
Any thoughts on this? Should we introduce some non-overloaded ?|
equivalent as I've done. Or do you see a different way to achieve this goal?
Well, that's unfortunate.
A cleaner solution would be to have StepOps
defined like :
trait StepOps[A, B] {
def orFailWith(failureHandler: B => Result):Step[A]
def ?|(failureHandler: B => Result): Step[A] = orFailWith(failureHandler)
def ?||(failureThunk: => Result): Step[A] = orFailWith(_ => failureThunk)
}
But that would obviously break existing user code
I like your suggested change. Maybe introduce it along with the scalaz
/cats
split? That update is going to break user code anyway.
Switching the thunk variant calls to ?||
is a bit of manual labor, but the compiler will catch them all so it's not a correctness concern.
Agreed, I've assigned it to 2.0
@vil1 The solutions detailed in https://github.com/Kanaka-io/play-monadic-actions/issues/16#issuecomment-206767907 seems to have been implemented now in v2.1. Is this issue fully resolved now?