Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

131–140 of 164 posts

Re: What if null was an Object in Java?

#131
post #91

Earlier quoted context omitted.

I might choose to rephrase that as "the problem is, some of these languages think that "" and null are equal." :-)

Isn't the lack of strict equality a result of loose typing in those languages?

Maybe - often more to do with overeager coercion, which does tend to go hand in hand with loose typing.

Re: What if null was an Object in Java?

#132

The 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

Also no new languages should default to mutable variables, fields, and types, when the language supposed to be in the same field as Java, eg. large enterprise services. And for both problems, they should be consistent, because now they are mess (there is an example in the article regarding nulls in collections, and for mutability a great example is collect(Collectors.toList()) vs toList()).

Re: What if null was an Object in Java?

#133
post #130

Earlier quoted context omitted.

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.

Scope functions are still huge. Yes, deep let chains can certainly be considered an antipattern (sometimes I like the approach of writing them, then transforming to more imperative for readability, I think readability peaks at a mix of imperative with some shallow .let), but I miss them in any language that is not kotlin. Variables should be the exception, not the norm. I have no patience for names that designate som…

> Scope functions are still huge.

I’d put any one of extension methods, value and data classes, immutable variables, structured concurrency, and top-level functions ahead of scope functions for reasons to switch to Kotlin. But hey, if you’re switching, we’re already friends :) .

Re: What if null was an Object in Java?

#134
post #125

Earlier quoted context omitted.

Seeing all this discussion reinforces the idea that no new programming language should have this "every type implicitly contains null" thing

Yes, sum types and generics solve this nicely. It's a shame Java was designed before they had generics, so the core of the language can't take advantage of them.

It’s still possible to do something like TypeScript, where everything is like generics in Java. But Java try to be consistent, while TypeScript purposefully not. There are a ton of features in TypeScript which are awesome, but it breaks in some rare cases. Java won’t allow that.

Re: What if null was an Object in Java?

#135
post #111

Earlier quoted context omitted.

> 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 Does nesting Option's really has practical use or does it quickly become confusing? In your example, Option > return type doesn't tell me by itself that this differentiates between a missing file or a missing setting. I would ne…

The biggest impact is on generic code - you don't want supplying Foo? as T rather than Foo to mean that there are side effects where successful returns and error cases are both represented by null.

In some cases (limited) nesting really seems useful. Nullable parameters for a Copywith method is another one (does null value mean 'no change' or 'set it to null').

The thing is that nullable covers majority of cases and is quicker to read and write (Foo? vs Option). Doing a?.b?.c is also more elegant than anything equivalent using an Option type.

Is there any languages that successfully combines both, nullable '?' syntax and an Option type?

Re: What if null was an Object in Java?

#136
post #128
post #47

Earlier quoted context omitted.

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…

Integers support both addition and multiplication and taking maximum and minimums, and a few other semi-group operations. Do you want to define different Null elements for all of them?

No, I don't want to define a representation for Null to all possible combinations of a set and a function/operation. That can be done by each developer if they see it fit and of the operations they want to have this.

But, for me, it makes sense to have a default representation for Null (that it is not automatically coerced—only if the developer explicitly asks for it) for one of the most common operations in that specific group.

Re: What if null was an Object in Java?

#137
post #133
post #130

Earlier quoted context omitted.

Scope functions are still huge. Yes, deep let chains can certainly be considered an antipattern (sometimes I like the approach of writing them, then transforming to more imperative for readability, I think readability peaks at a mix of imperative with some shallow .let), but I miss them in any language that is not kotlin. Variables should be the exception, not the norm. I have no patience for names that designate som…

> Scope functions are still huge. I’d put any one of extension methods, value and data classes, immutable variables, structured concurrency, and top-level functions ahead of scope functions for reasons to switch to Kotlin. But hey, if you’re switching, we’re already friends :) .

Really, top level functions? I fail to see their impact, outside of getting rid of ceremony around trivial code examples.

You missed optional and named arguments in the list, those really change cost/benefit decisions in API design. (and, unfortunately, make argument names part of the public interface, this must be the most controversial part in all of kotlin)

I singled out scope functions because all those other things feel very much like the usual set of language differences, whereas scope functions feel completely orthogonal. They could be added to every single language that has excursions and imperative elements, and they would be about the same improvement everywhere.

Re: What if null was an Object in Java?

#138

I like the way null is handled in Java, I think it’s pretty well thought out, other than the small awkwardness with basic types. Contrast that with SQL, which had my external scorn for null != null So you can do WHERE table.col = Null…. Ugh

SQL is one of the very few places where I deem null to be valid (altough, you could normalize all nulls away, most of the time it isn't worth it)

The null != null thing is something I quite like, as it simplifies a lot of joins/queries.

and as jraph said, null in relational databases has a very different meaning than null in most programming languages.

Don't know vs not assigned

Re: What if null was an Object in Java?

#139
post #130

Earlier quoted context omitted.

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.

Scope functions are still huge. Yes, deep let chains can certainly be considered an antipattern (sometimes I like the approach of writing them, then transforming to more imperative for readability, I think readability peaks at a mix of imperative with some shallow .let), but I miss them in any language that is not kotlin. Variables should be the exception, not the norm. I have no patience for names that designate som…

> Scope functions are still huge

They can be useful, but I can never remember which one of let, run, with, apply, and also I currently need. Also, I've noticed that they motivate overly "clever" code, especially but not exclusively in junior programmers.

Re: What if null was an Object in Java?

#140

The 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

Tony Hoare calls it (null) his Billion Dollar Mistake.

https://www.infoq.com/presentations/Null-References-The-Bill...

Post reply on HN