Problem
Obstacle not to use zero is the best practice. This article explains why and Guava : Declaring -hoc Error Handling, Ambiguous Semantics, Slow Failure, etc.
Writing a request occurs because of the need for a quick and clean failure when the prerequisites for calling the method are not met. The problem is that it replaces NPE with another exception (nonetheless more descriptive), as @puhlen explained.
The perfect solution
In an ideal world, path:Path would be identical to number:Int , and a test for the existence of an object would not be needed. The problem is that scala (like many other languages) allows zero violation of a purely object-oriented approach.
Interim solution
The java / scala compiler must force the Optional type as the only code that manages null and forces null to exist in the set system. In such cases, any use of null may be considered a compilation error. I do not know if this is completely possible.
Using @ NotNull / @ Nullable Annotations
Since there is no default behavior for the language / compiler, you will have an impedance mismatch between libraries.
Practical solution
Define your class with Predef2 with minimal template logic. I still get only one "Avoid using zero" or use guava Preconditions.checkNotNull
object Predef2{ def requireNotNull(object:AnyRef) = require(path != null, "Some object should not be null") } def doSomethingWithPath(path:Path) = { requireNotNull(path) ... }
source share