Suppress "discarded non-Unit value" warning
You suppress these warning by explictly returning unit (that is ()
).
By example turn this:
def method1() = {
println("Hello")
"Bye"
}
def method2() {
method1() // Returns "Bye", which is implicitly discarded
}
into:
def method1() = {
println("Hello")
"Bye"
}
def method2() {
method1()
() // Explicitly return unit
}
Now you can suppress value-discard warning via type ascription to Unit
in Scala 2.13.
This is an example:
def suppressValueDiscard(): Unit =
"": Unit
According to this answer, you can also use the syntax val _
, i.e.
def method2(): Unit = {
val _ = method1()
}
But there is some dispute over whether this or the answer by @Régis is the preferred style.