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

Overload resolution fails when a parameter requires an implicit conversion #3406

Closed
julienrf opened this issue Oct 30, 2017 · 2 comments
Closed

Comments

@julienrf
Copy link
Contributor

I’ve discovered this issue when working on the collections strawman.

import scala.reflect.ClassTag

// Base type introducing a flatMap method
trait Coll[A] {
  def flatMap[B](f: A => Coll[B]): Coll[B]
}

// An `Arr[A]` is not a `Coll[A]` ...
trait Arr[A]

// ... but there is an `ArrOps` decorator
trait ArrOps[A] extends Coll[A] {
  // we overload `flatMap` for the specific needs of `Arr`
  def flatMap[B : ClassTag](f: A => Coll[B]): Arr[B]
}

object Prelude {
  import scala.language.implicitConversions
  implicit def arrToArrOps[A](array: Arr[A]): ArrOps[A] = ???
}

object Usage extends App {
  
  import Prelude._
  
  val array: Arr[Arr[Int]] = ???
  
  // This line doesn’t compile
  array.flatMap(x => x)
  //  overloaded method value flatMap with alternatives:
  //    [B](f: Arr[Int] => Coll[B])(implicit evidence$1: scala.reflect.ClassTag[B])Arr[B] 
  //    [B](f: Arr[Int] => Coll[B])Coll[B]
  //   cannot be applied to (Arr[Int] => Arr[Int])
  
  // However if we explicitly trigger the implicit conversion to `Coll`, it compiles:
  array.flatMap(x => (x: Coll[Int]))
  
  // Also, if we remove the overload the first one compiles too (even though it requires
  // two implicit conversions: one to make the `flatMap` method available and one to make
  // the the result of the function passed to `flatMap` a `Coll`
  
}

Tested with Dotty 0.4.0-RC1. Scalac behaves exactly in the same way (see scala/bug#10576)

@allanrenucci
Copy link
Contributor

Reproduced on master

@odersky
Copy link
Contributor

odersky commented Jan 2, 2018

This is hard to fix without changing, and possibly breaking, a lot of other things. Since scalac behaves the same way, I am closing the issue.

@odersky odersky closed this as completed Jan 2, 2018
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

3 participants