Hello,
the compiler flag -Ywarn-value-discard (+ fatal warnings) is very helpful in avoiding quite subtle bugs which can occur due to the to-unit value coercion (a "classical" example in my case is doing a `map` instead of a `flatMap` when the result is `Future[Unit]`).
However, what about discarded expressions/values? E.g. this causes a warning:
scala> def g(): Int = 42
g: ()Int
scala> def h(): Unit = { g() }
<console>:8: warning: discarded non-Unit value
def h(): Unit = { g() }
^
h: ()Unit
But this doesn't:
scala> def h(): Unit = { g(); () }
h: ()Unit
Especially when doing more "functional"-style Scala, when most of the methods are pure, a discarded expression or value is usually a bug.
A less abstract example of what *might* be a bug is discarding a created future:
def f(): String = {
Future { /* computation */ }
"ok"
}
While this might be perfectly ok, I would say that usually in "production" code you would want to do something with the future, even if it's logging a potential error in onComplete.
When moving to more "functional" structures, such as scalaz/fs2's Task, this gets even worse:
def f(): String {
Task { /* computation */ }
"ok"
}
Now this definitely is a bug - there's no point in creating a Task which is never run. And so on ...
Question is: is adding discarded expression warnings a fundamentally bad idea? Maybe it would cause a flood of warnings in regular Scala code (though I don't think I see this happening)?
Or should I get to work and finish the plugin? ;)
--
Adam Warski