Navigation Menu

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

Compiler is confused by multiple apply()(implicit) if in the same statement #6630

Closed
scabug opened this issue Nov 8, 2012 · 4 comments
Closed
Assignees

Comments

@scabug
Copy link

scabug commented Nov 8, 2012

Compiler does not reevaluate apply's implicit each time apply is called, when the applications are in the same statement.

The compiler would be correct if test were a function, and the implicit was defined at the function definition site. In that case the implicit is only supposed to be evaluated in the first application to a function, where the type parameters are then fully determined.

However, this is not the case below, because the implicit is declared not for the function test but for the application function apply, and thus the type parameter A should be determined separately for each invocation of apply. It is proven as shown below by the fact that invoking apply with separate statements or even explicitly calling .apply more than once in the same statement does compile as expected.

The motivating use case is for a workaround to a use case that applies to bug SI-5550.

class Test {
  def apply[A](v: A)(implicit d: A <:< Any) = this
}
val test = new Test

scala> test(1)
res0: Test = Test@a541e5

scala> test("")
res1: Test = Test@a541e5

scala> test(1).apply("")
res2: Test = Test@105f21c

scala> test(1)("")
error: type mismatch;
 found   : java.lang.String("")
 required: <:<[Int,Any]
       test(1)("")
               ^
@scabug
Copy link
Author

scabug commented Nov 8, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6630?orig=1
Reporter: Shelby Moore III (shelby)

@scabug
Copy link
Author

scabug commented Nov 8, 2012

@paulp said:
This is a known consequence of the ability to pass implicit arguments explicitly.

@scabug scabug closed this as completed Nov 8, 2012
@scabug
Copy link
Author

scabug commented Nov 8, 2012

Shelby Moore III (shelby) said:
Ah I see, but then why doesn't enclosing in parenthesis indicate to the compiler that the function call should be completed and the implicit implicitly resolved?

I had tried the following before submitting this bug, which is why I think I didn't entertain the possibility you mentioned.

scala> ((test(None))(1))("2").array
error: type mismatch;
 found   : Int(1)
 required: <:<[DD[D[object scala.None]],DD[D[None] with D[Int] with D[String]]]
       ((test(None))(1))("2").array
                     ^

@scabug
Copy link
Author

scabug commented Nov 8, 2012

@paulp said:
"Extraneous" parentheses are discarded by the parser, so implicit resolution never sees them. I agree one could reasonably hope for it to be otherwise.

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

2 participants