Notice: As of Scala 2.11, NotNull
is deprecated.
As far as I understand, if you want a reference type to be non-nullable you have to mixin the magic NotNull
trait, and the compiler will automatically prevent you from putting null
-able values in it. See this mailing-list thread for instance.
What lacking is, a decent library support for non-nullable types. If I would like to write a package that don't need to interface java code directly, and I want to prevent all types in this package from using null
by default, I have no choice but to redefine all builting variables like so
//can't actually do that, but just to give the general idea
class NString extends String with NotNull
class NMap[X,Y] extends Map[X,Y] with NotNull
...
I expect scala to have (as a compiler plugin, or library) option for me to write
import collections.notnull._
in order to easily disallow null
usage in a specific scala file.
Is there an option to easily force many usefull types in the standard library to be not-nullable?
org.jetbrains.annotations.NotNull
– and I miss that as well. – Tactician