Reputation: 22840
While it is recommended to turn on compiler flags like -Wvalue-discard
or -Wunused:implicits
either explicitly or implicitly throught the use of sbt-tpolecat.
Sometimes you need to workarround those, but in a way that makes it explicit; since we generally consider such things bugs and that was the reason for using the compiler flags in the first place.
One, somewhat common, workarroud for those cases is the following void
function (courtesy of Rob Norris).
@inline final def void(args: Any*): Unit = (args, ())._2
However, such function has two problems.
Is there any other good workarroud that works out of the box?
Upvotes: 4
Views: 1191
Reputation: 22840
Since Scala 2.13
there are two ways to disable both warnings.
def testFix1()(implicit i: Int): Unit = {
val _ = i
val _ = data
}
def testFix2()(implicit i: Int): Unit = {
i : Unit
data : Unit
}
We do not have a formal reference or proof, but it is believed that the second option should be transparent; in the sense that it should not have any impact in runtime, like extra allocations or unwanted code generation.
You can see the code running here.
As far as we know, the same tricks should work on Scala 3 (aka Dotty).
???
Upvotes: 3