Skip to content

Order of implicits in file matters; could we at least fix this for implicit objects? #8697

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

Open
scabug opened this issue Jun 30, 2014 · 4 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) implicit infer
Milestone

Comments

@scabug
Copy link

scabug commented Jun 30, 2014

The following works:

trait T[_]
sealed trait A
sealed trait C


object A {
  implicit object AA extends T[A]
}

object C {
  implicit object CC extends T[C]
}

object B {
  implicitly[T[A]]
  implicitly[T[C]]
}

While the the following can not find the implicit T[C] instance:

trait T[_]
sealed trait A
sealed trait C


object A {
  implicit object AA extends T[A]
}

object B {
  implicitly[T[A]]
  implicitly[T[C]]
}

object C {
  implicit object CC extends T[C]
}

The only difference is the order of appearance for the B and C objects.

@scabug
Copy link
Author

scabug commented Jun 30, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8697?orig=1
Reporter: @puffnfresh
Affected Versions: 2.10.4, 2.11.1
See #3466

@scabug
Copy link
Author

scabug commented Jun 30, 2014

@retronym said:
Implicit objects are the same as implicit vals without explicit type annotations. We don't know there type without typechecking there body.

Implicit search will never force inference of an implicit down lower in the same file as the implicit call site. This avoids spurious cycles in inference.

Dotty, Martin's current research compiler, actually requires implicits to have explicit return types. IIRC, he does allow implicit objects. I'm not sure how he avoids the potential cycles.

I'll use this ticket as a reminder to dig into his approach and see if we can backport the treatment of implicit objects to Scala 2.12.

You can use explicitly type annotated implicit lazy vals as a more reliable tool.

@scabug
Copy link
Author

scabug commented Jun 30, 2014

@puffnfresh said:
@retronym thank you. That's a big help. I've been meaning to add lack of explicit types as a wart:

wartremover/wartremover#105

@scabug
Copy link
Author

scabug commented Mar 15, 2016

@SethTisue said:
there are a lot of tickets in this general space; see e.g. the links at #9674, #3466, etc.

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@smarter smarter added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Nov 17, 2018
rtyley added a commit to guardian/tagmanager that referenced this issue Oct 22, 2024
This is a precursor to:

* #536

Scala 2.13.11 added a warning that implicit definitions [should](
https://nrinaudo.github.io/scala-best-practices/tricky_behaviours/type_implicits.html) have an explicit type (because implicit resolution is already complicated enough for the compiler, and things like file-order can actually make a difference to whether implicit scopes are correctly searched: scala/bug#8697 (comment)):

* scala/scala#10083

The warning looks like this:

```
[error] ~/code/presence-indicator/app/autoscaling/Notification.scala:7:16: Implicit definition should have explicit type (inferred play.api.libs.json.Reads[autoscaling.Notification]) [quickfixable]
[error]   implicit val jsonReads = Json.reads[Notification]
[error]                ^
```

...this prepares us for Scala 3, where the explicit type is _required_.

More widely, beyond implicit definitions, in **library** code, the best practice is to always add an explicit type to all your **public** members, even when you're happy with what's being inferred - otherwise you can unintentionally break binary-compatibility just by changing the _implementation_ of a field:

* https://docs.scala-lang.org/scala3/guides/migration/incompat-type-inference.html
* https://nrinaudo.github.io/scala-best-practices/binary_compat/explicit_type_annotations.html
* https://scalacenter.github.io/scalafix/docs/rules/ExplicitResultTypes.html

## Automatically fixing this code issue

Scalafix actually does a better job than `-quickfix` for this particular task, because it adds imports if it needs to, so that you end up with this in your code:

```
implicit val jsonReads Reads[Notification] = Json.reads[Notification]
```

...rather than something like:

```
implicit val jsonReads Reads[com.gu.blah.foo.bar.Notification] = Json.reads[Notification]
```

### Fixing while still on Scala 2.12 - use Scalafix

In this commit, we're only trying to fix the implicit definitions, so I've added this in a new `.scalafix.conf` config file:

```
ExplicitResultTypes.onlyImplicits = true
```

The Scalafix rule needs to be run while the project is still on Scala 2.12, not Scala 2.13
(otherwise sbt will say: "Error downloading ch.epfl.scala:sbt-scalafix;sbtVersion=1.0;scalaVersion=2.13:0.13.0").

Once the Scalafix plugin is made available to sbt (by adding `addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0")`
to either `project/plugins.sbt` or `~/.sbt/1.0/plugins.sbt`), you can run these commands on the sbt prompt to automatically generate the changes in this PR:

```
scalafixEnable
scalafixAll ExplicitResultTypes
```
rtyley added a commit to guardian/tagmanager that referenced this issue Oct 22, 2024
This is a precursor to:

* #536

Scala 2.13.11 added a warning that implicit definitions [should](
https://nrinaudo.github.io/scala-best-practices/tricky_behaviours/type_implicits.html) have an explicit type (because implicit resolution is already complicated enough for the compiler, and things like file-order can actually make a difference to whether implicit scopes are correctly searched: scala/bug#8697 (comment)):

* scala/scala#10083

The warning looks like this:

```
[error] ~/code/presence-indicator/app/autoscaling/Notification.scala:7:16: Implicit definition should have explicit type (inferred play.api.libs.json.Reads[autoscaling.Notification]) [quickfixable]
[error]   implicit val jsonReads = Json.reads[Notification]
[error]                ^
```

...this prepares us for Scala 3, where the explicit type is _required_.

More widely, beyond implicit definitions, in **library** code, the best practice is to always add an explicit type to all your **public** members, even when you're happy with what's being inferred - otherwise you can unintentionally break binary-compatibility just by changing the _implementation_ of a field:

* https://docs.scala-lang.org/scala3/guides/migration/incompat-type-inference.html
* https://nrinaudo.github.io/scala-best-practices/binary_compat/explicit_type_annotations.html
* https://scalacenter.github.io/scalafix/docs/rules/ExplicitResultTypes.html

## Automatically fixing this code issue

Scalafix actually does a better job than `-quickfix` for this particular task, because it adds imports if it needs to, so that you end up with this in your code:

```
implicit val jsonReads Reads[Notification] = Json.reads[Notification]
```

...rather than something like:

```
implicit val jsonReads Reads[com.gu.blah.foo.bar.Notification] = Json.reads[Notification]
```

### Fixing while still on Scala 2.12 - use Scalafix

In this commit, we're only trying to fix the implicit definitions, so I've added this in a new `.scalafix.conf` config file:

```
ExplicitResultTypes.onlyImplicits = true
```

The Scalafix rule needs to be run while the project is still on Scala 2.12, not Scala 2.13
(otherwise sbt will say: "Error downloading ch.epfl.scala:sbt-scalafix;sbtVersion=1.0;scalaVersion=2.13:0.13.0").

Once the Scalafix plugin is made available to sbt (by adding `addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0")`
to either `project/plugins.sbt` or `~/.sbt/1.0/plugins.sbt`), you can run these commands on the sbt prompt to automatically generate the changes in this PR:

```
scalafixEnable
scalafixAll ExplicitResultTypes
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) implicit infer
Projects
None yet
Development

No branches or pull requests

4 participants