Scala compiler has -Xcheck-null
which tries to check if there are any potential null pointer dereference in runtime.
It's ok for me, but I get too much false positives, i.e. suppose I define logger :
private final val LOGGER: Logger = LoggerFactory.getLogger(classOf[GenericRestImpl])
The method getLogger
never returns null
. How can I pass this knowledge to compiler so it will not complain?
[WARNING] TestImpl.scala:31: warning: potential null pointer dereference: LOGGER.debug
[WARNING] LOGGER.debug("Using {} for sort", sortParam)
When I create new instance I can mark it with NotNull
trait:
return new Foo() with NotNull.
That's ok, but what to do with objects returned from other methods? Especially if it is comming from 3rd party library? I don't like idea to mark all my variables as Optional, because it will add too much overhead. Also, I don't like idea to create implicit conversions (because it will require extra class for each class that I want to mark as NotNull.
I also checked question Library support for Scala's NotNull trait but it didn't help to solve my problem.