If null-safety on the JVM is important to you, just use Kotlin.
What if null was an Object in Java?
101–110 of 164 posts
Re: What if null was an Object in Java?
#102Earlier quoted context omitted.
Null is the absence of a value. How do to distinguish 0 from no value?
The point is to eliminate the idea of an absence of a value. A variable is always assigned to a value, but there is a special value called null which behaves as a kind of sentinel value whose methods all return the null value for their respective type.
E.g a boolean in java has two states true/false while a Boolean with capital B has 3 states true/false/null.
In that context you can choose type to represent how many states you have. E.g if it’s a field representing a cache of a bool value you can represent “not yet calculated” with null. It you were to magically convert null to false for a Boolean it only has two states! It’s now unusable for the purpose.
Re: What if null was an Object in Java?
#103For me the given examples from Smalltalk are more appealing. I would trade null for nil, or at least have some nullsafe construction like .? in swift
Re: What if null was an Object in Java?
#104The problem isn't really that null isn't an object, it's that the type system allows any object reference of a particular class to also be null. After using Typescript for the past couple years, it's such a joy when you define a variable of type Foo and know it won't contain null. Granted, TS also has to deal with undefined vs null weirdness (and even more weirdness in that an object not containing a property is subt…
Turns out some ops without assignment and some printed messages would go into the pipeline (as in, to be piped in with `|`) and become part of the return value of the functions ; to make that work powershell changes the return type, appends the message or the non-assigned op to the hashtable and returns that as an object table.
To this day I'm not aware of any language doing this kind of thing, but maybe js does ? Anyway I fear I'm going to get some grey hair because of that.
Re: What if null was an Object in Java?
#105Earlier quoted context omitted.
Null should be valid. Kotlin solved Java's problem by making it a compiler error if a value that can be null isn't checked and shown to be null or the actual value, eliminating an entire class of exceptions.
I’m not familiar enough with kotlin to comment fully but from your description the checker framework [0] appears to do the same thing in Java. I confess I’m not fond of checker framework. I find the error messages can be obtuse but it is very effective. 0 - https://checkerframework.org/
The problem with nullability annotations in Java is that they are not enforced, and there is no commonly adopted standard. There are like ten competing libraries with similar annotations. There was JSR 305 ("Annotations for Software Defect Detection"), but it has been dormant. When you import a third party library, you never know what kind of nullability annotations it uses and if it uses them at all.
Re: What if null was an Object in Java?
#106What if every null reference was an instance of a Null Object pattern? https://en.wikipedia.org/wiki/Null_object_pattern Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs. Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as nee…
And since Objective-C contained a lot of C, it really just separated null dereferences from their causes making debugging much harder.
Re: What if null was an Object in Java?
#107Objective-c allows you to send messages to null objects. On one hand it allows for a form of null-coalescing, but on the other it allows bugs to slip in and get the program into an unexpected state, whereas a more rigorous treatment would result in a more deterministic crash.
It is part of what makes Objective-C phenomenally good for rapid development in the hands of competent developers. However, as team, and codebase sizes increase, it allows for subtle bugs by less competent developers.
Re: What if null was an Object in Java?
#108Earlier quoted context omitted.
Null is the absence of a value. How do to distinguish 0 from no value?
The point is to eliminate the idea of an absence of a value. A variable is always assigned to a value, but there is a special value called null which behaves as a kind of sentinel value whose methods all return the null value for their respective type.
You need a 257th value to have a byte sentinel.
Re: What if null was an Object in Java?
#109If null-safety on the JVM is important to you, just use Kotlin.
Just use Kotlin. I'm honestly not seeing any need nor benefit in writing Java anymore.
Re: What if null was an Object in Java?
#110Earlier quoted context omitted.
I'm going to be a bit pedantic here: There is a semantic difference between Option > and Option . If I intend to retrieve a setting from a file, the former allows me to differentiate between a missing file or a missing setting, while the latter destroys that information. i.e.: There are 3 possible cases, while only 2 can be represented. So T? doesn't compose, while Option does, which I'd consider a big difference. Ho…
That sounds like you should be using Result to handle the two E cases you are describing. Success with Ok(T), with Err(Missing File) plus Err(Missing Setting).
Which leads to a bit of an argument - nesting gives poor ergonomics, while collapsing impacts the ability to write generic code.