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

PartialFunction given as an argument won't compile if the argument is named #17155

Closed
makingthematrix opened this issue Mar 27, 2023 · 2 comments · Fixed by #17161
Closed

PartialFunction given as an argument won't compile if the argument is named #17155

makingthematrix opened this issue Mar 27, 2023 · 2 comments · Fixed by #17161

Comments

@makingthematrix
Copy link
Contributor

I have an overloaded method that takes a partial function as an argument.
Compilation fails if I specify the name of the argument when I call the method.
Two different errors in fact:

  1. that I'm missing parameter types if I don't give them, and
  2. that a total function was found instead of a partial one, if I do.

Compilation works if I remove the name of the argument or if I remove the unused overloaded method.

Compiler version

3.2.2 and 3.3.0-RC3

Minimized code

def foo[A, B](arr: Array[A], pf: PartialFunction[A, B]): Seq[B] = arr.toSeq.collect(pf)
def foo[A, B](list: List[A], pf: PartialFunction[A, B]): Seq[B] = list.collect(pf) // no errors if this is commented out

val arr = Array(1, 2, 3)
val resOkay = foo(arr = arr, { case n if n % 2 != 0 => n.toString }) // compiles
val resNope = foo(arr = arr, pf = { case n if n % 2 != 0 => n.toString }) // Error 1
val resNope2 = foo[Int, String](arr = arr, pf = { case n if n % 2 != 0 => n.toString }) // Error 2

Scastie: https://scastie.scala-lang.org/DgUgE2wrRWmY3lzwmp30xQ

Output

Error 1:

Missing parameter type

I could not infer the type of the parameter x$1 of expanded function:
x$1 =>
  x$1 match 
    {
      case n if n % 2 != 0 =>
        n.toString
    }.
    val resNope = foo(arr = arr, pf = { case n if n % 2 != 0 => n.toString }) // error

Error 2:

Found:    Int => String
Required: PartialFunction[Int, String]

Expectation

Ideally, all calls to foo in the code above should compile without errors.

@makingthematrix makingthematrix added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 27, 2023
@makingthematrix
Copy link
Contributor Author

makingthematrix commented Mar 27, 2023

By the way, in Scala 2 (2.13.10), it's the third call (resNope2) which works, while the first two don't. But in this case, it works both with and without named argument.

@makingthematrix
Copy link
Contributor Author

That was quick. Thank you!

@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants