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

Improved type inference for multiple parameter lists #9262

Closed
scabug opened this issue Apr 5, 2015 · 4 comments
Closed

Improved type inference for multiple parameter lists #9262

scabug opened this issue Apr 5, 2015 · 4 comments
Assignees

Comments

@scabug
Copy link

scabug commented Apr 5, 2015

object Foo {
  def bar[A](a1: A)(a2: A) = {}
}

Foo.bar(Left(1))(Right(2))

does not compile:

error: type mismatch;
 found   : scala.util.Right[Nothing,Int]
 required: scala.util.Left[Int,Nothing]
              Foo.bar(Left(1))(Right(2))
                                    ^

If a single parameter list is used, A is inferred as Either[Int, Int], as hoped.

object Foo {
  def bar[A](a1: A, a2: A) = {}
}

Foo.bar(Left(1), Right(2))

A real-world example of this is Option.fold, which requires an explicit type in circumstances where pattern-matching, or map/getOrElse would not.

@scabug
Copy link
Author

scabug commented Apr 5, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9262?orig=1
Reporter: Paul Draper (draperp)
Affected Versions: 2.11.6

@scabug
Copy link
Author

scabug commented Apr 5, 2015

@soc said:
I'd tentatively say that this behavior works as intended. The idea is that the type inference looks at one parameter list as a whole, but considers each of them one after the other.

@scabug
Copy link
Author

scabug commented Apr 9, 2015

@retronym said:
Yes, this behaviour or inference runs pretty deeply through the way that applications are type checked, and it is relied upon in APIs like foldLeft, which allow the constraints accumulated by considering the first argument list to allow you to omit types in the second argument list.

def foo[A, B](a: A)(f: A => B)
foo(1)(x => -x) // no need to write `x: Int => -x` here

@SethTisue
Copy link
Member

SethTisue commented Dec 4, 2017

fwiw, Dotty compiles both Option(5).fold(Left("err"))(Right(_)) and the Foo.bar example just fine and infers the right types

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