After switching compiler from 3.3.0 to 3.3.1 I started getting a lot of “unset private variable” warnings (hundreds in a single project). This is typically related to code that is using JavaFX and
@FXML annotations, like here:
private var button : javafx.scene.control.Button = _
@FXML will cause assignment of proper value to
button based on declaration in related
.fxml file, but Scala compiler is not aware of that and is issuing a warning.
Is there a proper way to disable those new warnings?
scalac -W says
scalac -Wunused:privates, so
-Wunused:-privates to disable.
-Wconf:msg=unset private var:s to silence the warning.
Scala 3 doesn’t have the fine-grained category support for messages in Scala 2.
This problem also arises when you do give the variable a value:
class Test(x: Int) :
private var y = x
def show(): Unit = println(y)
unset private variable on the second line.
Isn’t that a bug, as in false positive warning (“here comes the wolf”) that should be reported?
It is after all not unset…
Yes, i think so, have made a report.
Btw, it does not always produce such a warning, I have other private variables where this does not happen. I was not able to find a pattern yet.
I commented on the ticket that Scala 2 says to make it a val.
At issue is not whether the var is initialized with underscore or an explicit value; underscore just means the default value.
It’s warning that
button_= is never called.
If the field is set only reflectively, then warnings are – what’s the word for when you subvert the type system and everything the language puts in place to help you write sound code?
The word “subvert” is too tame.
Thank you for pointing this out. I should have waited for more opinions before submitting the issue. My apologies.