Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala 2.12 -> 2.13 regression in implicit precedence #11596

Closed
oxbowlakes opened this issue Jun 27, 2019 · 4 comments
Closed

Scala 2.12 -> 2.13 regression in implicit precedence #11596

oxbowlakes opened this issue Jun 27, 2019 · 4 comments

Comments

@oxbowlakes
Copy link

oxbowlakes commented Jun 27, 2019

The following code compiles under scala 2.12.8 but not under scala 2.13.0 (it fails with ambiguous implicit warnings)

package object foo {
  import scala.language.higherKinds
  class Functor[X[_]]
  trait Monad[F[_]] extends Functor[F]

  class OptionT[M[_], A]
  trait OptionTInstances2 {
    implicit def optionTMonad[M[_]]: Monad[({ type l[a] = OptionT[M, a] })#l] = ???
  }

  trait OptionTInstances1 extends OptionTInstances2 {
    implicit def optionTFunctor[M[_]]: Functor[({ type l[a] = OptionT[M, a] })#l] = ???
  }

  object OptionT extends OptionTInstances1

  object Main extends App {
    implicitly[Functor[({type l[a] = OptionT[Option, a]})#l]]
  }
}
@som-snytt
Copy link

I see 2.12 doesn't even look at the monad implicit, so it may have progressed with fixes to "implicit shadowing" (just a guess) or maybe pruning strategy.

I'm actually not able to view the spec right now, for some internet reason. That's a reason to finally get a PDF version again. I know someone worked on that recently. (Edit: it loaded after several minutes. Cloudflare?)

If both methods are in scope, ordinary static overload resolution applies. IIRC "defined in a subclass" is a tie-breaker?

Just to add, it was an opportunity to compare option names:

~/scala-2.12.8/bin/scalac -Xdev -Ydebug -Ytyper-debug -Xprint:typer owbox.scala
scalac -Xdev -Vimplicits -Vdebug -Vtyper -Vprint:typer owbox.scala

I do like how the Vs align. V means I'm asking for more info, hopefully without changing behavior (although forcing typechecking can side effect).

Also apologies to oxbowlakes for mistyping owbox. From the crossword, I recently learned "sin bin", so for issues I hereby propose "ow box".

@joroKr21
Copy link
Member

joroKr21 commented Jun 27, 2019

That's intentional. See scala/scala#6123 and scala/scala#6441

There is a natural prioritization because Monad extends Functor. Placing the functor instance in a subclass evens the odds so to speak. The solution is to swap places when you want to cross compile or just remove the boilerplate for 2.13.+

Edit: That makes me think, I guess "defined in a subclass" is not simply a tie-breaker, it gives you an extra point, but so does "returning a subtype".

@som-snytt
Copy link

Thanks @joroKr21 ! "Aha!"

@xuwei-k
Copy link

xuwei-k commented Jun 27, 2019

#11068

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

No branches or pull requests

6 participants