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

Type inference fails in overloaded arguments with functions #9485

Closed
scabug opened this issue Sep 23, 2015 · 4 comments
Closed

Type inference fails in overloaded arguments with functions #9485

scabug opened this issue Sep 23, 2015 · 4 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Sep 23, 2015

trait Traversable[+A] {
  def flatMap[B](f: A => Traversable[B]): Traversable[B] = ???
}

trait Iterable[+A] extends Traversable[A] {
  def flatMap[B](f: A => Iterable[B]): Iterable[B] = ???
}

trait Seq[+A] extends Iterable[A] {
  def flatMap[B](f: A => Seq[B]): Seq[B] = ???
}

object Test extends App {
  val a: Seq[Int] = new Seq[Int] {}
  val b: Iterable[Int] = new Iterable[Int] {}
  a.flatMap(i => b) //!! changing the argument to ((i: Int) => b) works 
}

The type of the argument of the method flatMap is one of Int=>Traversable[Int]; Int=>Iterable[Int] or Int=>Seq[Int].
Obviously the type of the variable i must be Int, however the type inferrer fails to infer this, causing the compilation to fail. Once designated the type of i as Int, the compilation went on smoothly.

I think the type would be rather obvious to be inferred but Scala compiler cannot.

@scabug
Copy link
Author

scabug commented Sep 23, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9485?orig=1
Reporter: Tongfei Chen (ctongfei)
Affected Versions: 2.11.7

@scabug
Copy link
Author

scabug commented Sep 24, 2015

@som-snytt said:
Overload resolution requires the arg be typechecked with an undefined expected type.

@diesalbla
Copy link

This is already solved in the 2.13.x and the 2.12.8 branches.

@som-snytt
Copy link

I noticed it was improved early in 2.12, in the long progression of function inference improvements.

@SethTisue SethTisue modified the milestones: 2.12.8, 2.12.0 Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants