Conversation
4c504f9 to
c9811fa
Compare
rtyley
added a commit
that referenced
this pull request
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
that referenced
this pull request
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 ```
c9811fa to
b7e6f41
Compare
JamieB-gu
approved these changes
Oct 22, 2024
rtyley
commented
Oct 22, 2024
| val orderedSponsorships: List[Sponsorship] = orderBy match { | ||
| case("sponsor") => sponsorships.sortBy(_.sponsorName) | ||
| case("from") => sponsorships.sortBy(_.validFrom.map(_.getMillis).getOrElse(0l)) | ||
| case("from") => sponsorships.sortBy(_.validFrom.map(_.getMillis).getOrElse(0L)) |
Member
Author
There was a problem hiding this comment.
Scala 2.13 now warns on using a lower-case l for Long literals: scala/scala#7685
[warn] /Users/Roberto_Tyley/code/tagmanager/app/modules/clustersync/NodeStatusRepository.scala:40:8: [rewritten by -quickfix] Lowercase el for long is not recommended because it is easy to confuse with numeral 1; use uppercase L instead
[warn] 1l
[warn] ^
4 tasks
Member
Author
|
This has been deployed to PROD, and looks good! ✨ |
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR sits on top of:
ProcedureSyntax- usedef x(): Unit = {...}#523scala-collection-compat#526ExplicitResultTypesfor implicit definitions #538...with all those updates out of the way, this PR to upgrade to Scala 2.13 is pretty small:
mapVto replacemapValuescollection.breakOutno longer exists - it was just a way of converting a collection to particular type, and we can just saytoMaphere instead.scala.collection.convert.wrapAll._goes toscala.jdk.CollectionConverters._Completing this upgrade is also a step in unblocking:
Testing
This has been deployed to CODE, and looks good: