|
1 |
| -# scalac-compat |
| 1 | +# scalac-compat |
| 2 | + |
| 3 | +*scalac-compat* is a <sub>micro</sub>library containing a set of lightweight tools designed to tackle mismatches between different Scala compiler versions in cross-build projects. |
| 4 | + |
| 5 | +Currently it consists of a single module *scalac-compat-annotation*. |
| 6 | + |
| 7 | +## scalac-compat-annotation |
| 8 | + |
| 9 | +The module contains Scala version specific annotations that can help to tune a build in cases when different Scala compiler versions may treat same standard annotations differently. |
| 10 | + |
| 11 | +### Usage |
| 12 | + |
| 13 | +The module is published for Scala 2.12.x, 2.13.x and 3.2.x: |
| 14 | +```scala |
| 15 | +// sbt |
| 16 | +"org.typelevel" %% "scalac-compat-annotation" % "{{MODULE_VERSION}}" |
| 17 | + |
| 18 | +// mill |
| 19 | +ivy"org.typelevel::scalac-compat-annotation:{{MODULE_VERSION}}" |
| 20 | + |
| 21 | +// Scala CLI |
| 22 | +//> using lib "org.typelevel::scalac-compat-annotation:{{MODULE_VERSION}}" |
| 23 | +``` |
| 24 | +Then import the module annotations: |
| 25 | +```scala |
| 26 | +import org.typelevel.scalaccompat.annotation._ |
| 27 | +``` |
| 28 | + |
| 29 | +#### `@nowarn` annotation specializations |
| 30 | + |
| 31 | +There are a set of specializations defined for `@scala.annotation.nowarn` that either redirect to the standard annotation or to a no-op annotation depending on a particular Scala version: |
| 32 | + |
| 33 | +| Scala<br>version | `@nowarn2` | `@nowarn212` | `@nowarn213` | `@nowarn3` | |
| 34 | +| ---- | ---------- | ------------ | ------------ | ---------- | |
| 35 | +| 2.12 | `@nowarn` | `@nowarn` | no-op | no-op | |
| 36 | +| 2.13 | `@nowarn` | no-op | `@nowarn` | no-op | |
| 37 | +| 3 | no-op | no-op | no-op | `@nowarn` | |
| 38 | + |
| 39 | +##### Example |
| 40 | + |
| 41 | +Consider a case when someone has to deal with `scala.collection.Stream` in a cross-build project. `Stream` is deprecated since Scala v2.13. However, simply applying `@nowarn("cat=deprecation")` to methods that attempt to reference to `Stream` may lead to another warning on Scala version prior to v2.13: |
| 42 | +``` |
| 43 | +@nowarn annotation does not suppress any warnings |
| 44 | +``` |
| 45 | +Thus the specialized `@nowarn` annotations can come in handy here: |
| 46 | +```scala |
| 47 | +import org.typelevel.scalaccompat.annotation._ |
| 48 | + |
| 49 | +@nowarn213("cat=deprecation") |
| 50 | +@nowarn3("cat=deprecation") |
| 51 | +def reinventTheWheel: Stream[Int] = { |
| 52 | + Stream |
| 53 | + .iterate((1, 1)) { case (a2, a1) => (a1, a2 + a1) } |
| 54 | + .map(_._1) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### `@unused` compatibility annotation |
| 59 | + |
| 60 | +The `@scala.annotation.unused` annotation was introduced since Scala v2.13.x and does not exists in previous versions of Scala library. The compatibility `@org.typelevel.scalaccompat.annotation.unused` annotation simply redirects to the `@scala.annotation.unused` on versions where it exists while mimics the same behavior with `@scala.annotation.nowarn` for Scala v2.12.x: |
| 61 | + |
| 62 | +| Scala<br>version | `@scalaccompat.annotation.unused`<br>redirects to | |
| 63 | +| ---- | ---------------------------------------- | |
| 64 | +| 2.12 | `@scala.annotation.nowarn("cat=unused")` | |
| 65 | +| 2.13 | `@scala.annotation.unused` | |
| 66 | +| 3 | `@scala.annotation.unused` | |
| 67 | + |
| 68 | +##### Example |
| 69 | + |
| 70 | +```scala |
| 71 | +// Notice that there is no need to import `scala.annotation.unused`. |
| 72 | +import org.typelevel.scalaccompat.annotation._ |
| 73 | + |
| 74 | +// Compiles on all supported Scala versions. |
| 75 | +def testUnusedParam(name: String, @unused unusedParam: String): Unit = { |
| 76 | + // The `unusedParam` is not used withing the body. |
| 77 | + println(s"Hello, $name") |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +See more examples in **./annotation/src/test** |
| 82 | + |
| 83 | +## Conduct |
| 84 | + |
| 85 | +Participants are expected to follow the [Scala Code of Conduct](https://www.scala-lang.org/conduct/) while discussing the project on GitHub and any other venues associated with the project. See the [organizational Code of Conduct](https://github.com/typelevel/.github/blob/main/CODE_OF_CONDUCT.md) for more details. |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +All code in this repository is licensed under the Apache License, Version 2.0. See [LICENSE](./LICENSE). |
0 commit comments