Hacker News new | ask | show | jobs
by krat0sprakhar 2461 days ago
> Nullable enables you to directly target the flaws in code that lead to NullReferenceException. The lowest layer of the framework libraries has been annotated, so that you know when to expect null.

I hope Java gets this one day.. would be a cool feature

3 comments

In the JVM land you can check out Kotlin, which does this.
You can try checkerframework [1], which uses annotations to perform extra typechecking at compile time, including nullable/not-nullable checks. Works properly altough it has a few issues: the biggest is that support is still stuck at java 8, and the framework is sometimes not smart enough to see that you already checked if a nullable field is null or not and you can safely use it.

[1] https://checkerframework.org/

Annotations don't work for local variables, though. So you only get nullability checking around calling methods and accessing fields, right?
Annotations in general do work for local variables, although this is specified on a per-annotation basis.

If I remember correctly, for example, the @NotNull annotation in javax.validation.constraints cannot be used for a local variable, but the one provided by the checkerframework can.

Optional ? But redoing the standard lib would be no go in javaland ? It's the non nullable reference I find interesting.
The API hasn’t changed, and remains backwards compatible. However it has been annotated, so that when targeting it with a compiler configured to validate/verify nullability you get a better (read: safer) experience. It can configured as an error or a warning.

MS does this sort of thing a good amount, preserving backwards compatibility but enhancing the experience.