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

apply() and update() don't always permit shorthand operators when apply() takes an implicit #6714

Closed
scabug opened this issue Nov 26, 2012 · 6 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Nov 26, 2012

When both apply() and update() are present and apply() takes a simple parameter and an implicit parameter, shorthand operators like += produce a compile error. See the following example.

class ImplicitThing

class Foo {
  def apply(x: Int)(implicit imp: ImplicitThing) = { println("read"); 123 }
  def update(x: Int, y: Int)(implicit imp: ImplicitThing) { println("updated") }
  
  // But the following alternative works!
  //def apply[T](x: T)(implicit imp: ImplicitThing) = { println("read"); 123 }
  //def update[T](x: T, y: T)(implicit imp: ImplicitThing) { println("updated") }
}

object TestCase extends App {
  implicit val imp = new ImplicitThing
  val foo = new Foo
  foo(3)               // works
  foo(3) = 4           // works
  foo(3) = foo(3) + 4  // works
  foo(3) += 4          // <-- compile error
}

The error is as follows.

../src/TestCase.scala:18: error: type mismatch;
 found   : ImplicitThing
 required: Int
  foo(3) += 4
     ^
one error found

Removing the implicit parameter from apply() fixes the error. It doesn't matter whether update() has the implicit parameter. Also, the version with type parameters works for some reason. All cases behaved the same with both 2.9.1 and 2.10.0 RC2.

@scabug
Copy link
Author

scabug commented Nov 26, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6714?orig=1
Reporter: Martin Pärtel (mpartel)
Affected Versions: 2.9.1, 2.10.0-RC2

@scabug
Copy link
Author

scabug commented Nov 26, 2012

Martin Pärtel (mpartel) said:
Probably shouldn't be a Blocker but it wouldn't let me change it.

@scabug
Copy link
Author

scabug commented Nov 26, 2012

@adriaanm said:
Nada, would you like to take a look? Feel free to re-assign to me.

@scabug
Copy link
Author

scabug commented May 20, 2013

@JamesIry said:
2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

@scabug
Copy link
Author

scabug commented Jun 23, 2016

@adriaanm said:
Looks like an issue with retypechecking, since it works in the polymorphic case?

@scabug
Copy link
Author

scabug commented Mar 10, 2017

@som-snytt said:
More info on the linked ticket.

In the polymorphically perverse case, it checks but doesn't do anything expected.

scala/scala#5770

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