I think the nullable reference type system in c# is a perfectly workable compromise for languages that had nullability from the very beginning. Once a code base uses them fully, most null-related bugs are gone. And it’s far from an unwieldy bolt-on.
It's funny because I really dislike the C# approach. It's not truly enforced - any code with nullable disabled can call public functions with `null`, even if on your side they were marked as non-null.
What if null was an Object in Java?
121–130 of 164 posts
Re: What if null was an Object in Java?
#122Kotlin makes nullability part of the typesystem. It's a good reference for what would happen if Java did this better because if you are using Java you can just switch to Kotlin and experience this in your own code base (you can mix Java and Kotlin code easily). You can actually write extension functions for nullable types and calling them on a null value does not cause a Nullpointer exception. This also works with Ja…
In java I can make my own static function
isNullOrBlank(String input)
Which works on nulls too, right?
It's annoying to have to reference another class - but otherwise it's not much less ergonomic.
Re: What if null was an Object in Java?
#123Kotlin makes nullability part of the typesystem. It's a good reference for what would happen if Java did this better because if you are using Java you can just switch to Kotlin and experience this in your own code base (you can mix Java and Kotlin code easily). You can actually write extension functions for nullable types and calling them on a null value does not cause a Nullpointer exception. This also works with Ja…
Re: What if null was an Object in Java?
#124Kotlin makes nullability part of the typesystem. It's a good reference for what would happen if Java did this better because if you are using Java you can just switch to Kotlin and experience this in your own code base (you can mix Java and Kotlin code easily). You can actually write extension functions for nullable types and calling them on a null value does not cause a Nullpointer exception. This also works with Ja…
CharSequence?.isNullOrBlank() In java I can make my own static function isNullOrBlank(String input) Which works on nulls too, right? It's annoying to have to reference another class - but otherwise it's not much less ergonomic.
Re: What if null was an Object in Java?
#125The 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…
Seeing all this discussion reinforces the idea that no new programming language should have this "every type implicitly contains null" thing
It's a shame Java was designed before they had generics, so the core of the language can't take advantage of them.
Re: What if null was an Object in Java?
#126Kotlin makes nullability part of the typesystem. It's a good reference for what would happen if Java did this better because if you are using Java you can just switch to Kotlin and experience this in your own code base (you can mix Java and Kotlin code easily). You can actually write extension functions for nullable types and calling them on a null value does not cause a Nullpointer exception. This also works with Ja…
CharSequence?.isNullOrBlank() In java I can make my own static function isNullOrBlank(String input) Which works on nulls too, right? It's annoying to have to reference another class - but otherwise it's not much less ergonomic.
Re: What if null was an Object in Java?
#127I think the nullable reference type system in c# is a perfectly workable compromise for languages that had nullability from the very beginning. Once a code base uses them fully, most null-related bugs are gone. And it’s far from an unwieldy bolt-on.
I think there is a way to migrate to an union type myType | null. Instead.
Eg they can't distinguish between one or two layers of nullable. Or between null and an optional null.
(In eg Rust syntax between `Option>` vs `Option`. Or `()` vs `Option`, where `()` would be how you spell `null` in Rust.)
Using tags is simpler and cleaner.
Re: What if null was an Object in Java?
#128Ruby has a `NilClass` and the best/worst part of it is the to_s is "", to_i is 0, to_f is 0.0, to_a is [], to_h is {} It's incredibly clean and convenient until you wake up one morning and have no idea what is happening in your code
I dont think there is something wrong with that once you think about what is a Null Element (or identity) in a group that is represented by a set of elements and a function: Integer, + => 0 Float, + => 0.0 Array, add => [] Hash, merge => {} and so on. I think maybe we can debate the operations/functions, but they make sense. For Integer in some ways you can define almost all other operations that you commonly use bas…
Re: What if null was an Object in Java?
#129We already have None in e.g. Python but that merely means that "x.mathod_name()" instead of throwing a NullPointerException raises an AttributeError, because None has no method "method_name". Okay? Not really any meaningfully different.
Re: What if null was an Object in Java?
#130Earlier quoted context omitted.
Just use Kotlin. I'm honestly not seeing any need nor benefit in writing Java anymore.
I‘d say its the other way around, Java is closing the gap, and I say that as a Kotlin fan. Nullability in the type system is the big remaining advantage.
Variables should be the exception, not the norm. I have no patience for names that designate some fluid work in progress instead of a value.