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

Unapplied method conversion error is not consistent when an implicit conversion is applied #11851

Open
OndrejSpanel opened this issue Jan 17, 2020 · 2 comments
Milestone

Comments

@OndrejSpanel
Copy link
Member

Following code demonstrates an inconsistent error with Scala 2.13.1.

The code uses an implicit conversion to adapt a function to a different signature. The underscore is required only when the result of the implicit conversion is a function with a different number of parameters.

I think the error should not be given at all, or in both situations. I am not sure which of those would be more logical.

import scala.language.implicitConversions

object Main extends App {
  def function2_0(i: Int, j: Int): String = j.getClass.getSimpleName
  def function2_1(i: Int, j: Double): String = j.getClass.getSimpleName

  def function3_0(i: Int, j: Int, k: Int): String = j.getClass.getSimpleName
  def function3_1(i: Int, j: Double, k: Int): String = j.getClass.getSimpleName

  implicit def fromMy2[T](f: (Int, T) => String): (Int, Any, Int) => String = (i, a, _) => f(i, a.asInstanceOf[T])
  implicit def fromMy3[T](f: (Int, T, Int) => String): (Int, Any, Int) => String = (i, a, k) => f(i, a.asInstanceOf[T], k)

  def chooseAFunction(a: Int): (Int, Any, Int) => String = {
    a match {
      case 20 => function2_0 
      case 21 => function2_1
      case 30 => function3_0
      case 31 => function3_1
    }
  }

  val f30 = chooseAFunction(30)
  val f31 = chooseAFunction(31)
  println(f30(0, 1, 2))
  println(f31(0, 1.0, 2))
}

Compiler output:

[info] Compiling 1 Scala source to W:\test\Sandbox\target\scala-2.13\classes ...
[error] W:\test\Sandbox\src\main\scala\Main.scala:15:18: missing argument list for method function2_0 in object Main
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `function2_0 _` or `function2_0(_,_)` instead of `function2_0`.
[error]       case 20 => function2_0
[error]                  ^
[error] W:\test\Sandbox\src\main\scala\Main.scala:16:18: missing argument list for method function2_1 in object Main
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `function2_1 _` or `function2_1(_,_)` instead of `function2_1`.
[error]       case 21 => function2_1
@Jasper-M
Copy link
Member

Jasper-M commented Jan 17, 2020

I guess the error should read

Unapplied methods are only converted to functions when a function type of the same arity is expected.

I'm not sure whether that's in line with the spec though.

@som-snytt
Copy link

Under -Xsource:214:

test/files/neg/t11851.scala:20: warning: Eta-expansion performed to meet expected type (Int, Any, Int) => String, which is SAM-equivalent to <notype>,
even though trait Function3 is not annotated with `@FunctionalInterface`;
to suppress warning, add the annotation or write out the equivalent function literal.
      case 20 => function2_0
                 ^
test/files/neg/t11851.scala:21: warning: Eta-expansion performed to meet expected type (Int, Any, Int) => String, which is SAM-equivalent to <notype>,
even though trait Function3 is not annotated with `@FunctionalInterface`;
to suppress warning, add the annotation or write out the equivalent function literal.
      case 21 => function2_1
                 ^

I won't pretend to be unconfused.

@lrytz lrytz added this to the Backlog milestone Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants