play-monadic-actions icon indicating copy to clipboard operation
play-monadic-actions copied to clipboard

inference with overloaded `?| ` operator

Open drbild opened this issue 8 years ago • 4 comments

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?

drbild avatar Apr 05 '16 22:04 drbild

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

vil1 avatar Apr 07 '16 08:04 vil1

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.

drbild avatar Apr 07 '16 15:04 drbild

Agreed, I've assigned it to 2.0

vil1 avatar Apr 07 '16 16:04 vil1

@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?

marko-asplund avatar Aug 30 '20 21:08 marko-asplund