Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I'd think that eliminating Nulls is a bandaid over the wrong problem.

That's not what is happening here. The issue with the current iteration of Java is that all type declarations implicitly include `null`. So you can do for example:

    // some method
    public String getSomeValue();

    // client code
    String someVal = obj.getSomeValue();
At that point, the caller of the getSomeValue() can't depend on someVal being non-null. You say "Any code calling a function that can return a Null should know that being handed a Null is a possibility and handle it, right?" and that's really the problem. ANY method that returns an object type can return null, so, in theory, it would force all callers to do stuff like `if (someVal != null) ...`, which is really annoying if it has to be all over the place.

This proposal makes it clear to callers whether a method can return null, or if it is guaranteed by the type system not to return null. That way, it is now possible for a method to state "I guarantee that I will only return a String, never null", and previously that was not possible (directly in the language at least - there were some kludgey workarounds like the Optional generic type). So this means that clients don't need to do tons of unnecessary null checks.

This is definitely not an issue specific to Java. For example, TypeScript is clear that its types do NOT automatically include null, and it makes coding SO much clearer. For a type to include null (or undefined, but that's a whole other JS-specific ball of wax...) you must explicitly write e.g.:

    const foo: string | null;


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: