Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

41–50 of 164 posts

Re: What if null was an Object in Java?

#41
post #10

Ruby 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

Of course since it's Ruby you can just monkey patch those to_s methods to do whatever the hell you want, confounding anyone else working on your codebase.

I love using Ruby when I'm the only one who will ever have to look at or touch it.

Re: What if null was an Object in Java?

#42
post #8

Earlier quoted context omitted.

An Optional is just a tri-valued null (null, None, and Some), so no. It'd be nice if Java had a concept of a never-null reference (like a C++ reference vs. a C++ pointer), but the @NotNull annotation wasn't enforced the last time I checked. Also, there's no way for an object to express that invariant because encapsulation is so weak. Given only this constructor (and no reflection): Foo() { foo.bar = new Bar(); /* foo…

Wait, javas Optional is a reference type so it can be null? Doesn’t that almost defeat the purpose of it?

It doesn’t defeat the problem in theory, but in my experience it does in practice. I’ve never come across an NPE on a nullable reference even in development - it would have to be the result of a really fundamental misunderstanding of the concept.

YMMV. Obviously it depends on your teammates.

Re: What if null was an Object in Java?

#44
post #11

Earlier quoted context omitted.

The Nullable class is nice and I love to use it but it would be even better with an option type like in F#

There isn’t any big difference between T? And Option semantically. Many code bases use an Option in C# to indicate a 0-or-1 object result, but refactor those to T? instead with nullable. It combines better e.g Option > doesn’t need to be handled manually.

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.

However, without a builtin option type, duck-typing, or a pleasant way of converting the types, Option may become a hassle (especially when different dependencies ship their own). And as T? is shipped with the language this is probably why it is used when the composability is not required.

P.S.: In C# T? even neatly composes.

  return obj?.a?.b
is equivalent to

  if (obj != null && obj.a != null) {
    return obj.a.b
  } else return null;

Re: What if null was an Object in Java?

#46
post #25
post #17

Earlier quoted context omitted.

Assuming that was the only constructor you defined on class Foo, and you used this.bar instead of foo.bar (latter won't compile), then the caller can't possibly get a Foo with bar set to null (except by reflection, and there are ways to prevent that). Moreover, even if new Bar() did throw an (unchecked) exception, the invariant would still hold, since Foo would rethrow the exception. This has always been the case, as…

Doing it requires two threads. Thread A sets a shared reference to a newly allocated and null initialized reference to Foo: shared = new Foo(); While that's running, thread B invokes a method on the reference that assumes bar is non-null: shared.useBar(); // null pointer exception Later, thread A runs the constructor for Foo.

By that standard, C and C++ are much worse, since they offer no runtime encapsulation at all, and have much worse and more subtle multithreaded errors (e.g. Java at least guarantees that all native word sized reads/writes are atomic, if I recall correctly). C++ doesn't even guarantee that a reference can't be null, or worse, deallocated before it is dereferenced. They allow you to specify that a field is of some type and shouldn't be null, which is nice, but they don't enforce that in any way, they just call any code path that violates it UB.

For example, this is code that any C or C++ compiler will happily run and do something:

  struct Bar {
    int b;
  };
  struct Foo {
    struct Bar bar;
  } foo;

  strcpy((char*)(&foo), "ABC");
Or in relation to null C++ references:

  int& foo(int* p) {
    return *p;
  } 
  
  int &r = foo(nullptr); //UB, but in practice will likely result in a null reference at runtime
Similarly, accessing an object from multiple threads without synchronization means its value is not fully defined in Java. Unlike C or C++, it is at least known to be a Java type, not a memory corruption vulnerability.

Re: What if null was an Object in Java?

#47
post #10

Ruby 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 based on the addition.

So while nil is an object when trying to find a representation in other group I find it logical or expected.

Also Ruby will not automatically try to coerce nil when not asked to do so

like for example 0 + nil will throw an error.

Re: What if null was an Object in Java?

#48
Objective-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.

Re: What if null was an Object in Java?

#49
post #6

If Null is not a subtype of MyType, then you wouldn't be able to assign null to a variable decalred as MyType, without breaking the rest of the rules of Java. I don't really see how this could work, even theoretically.

There's no reason that something can't both an object and the bottom of the type hierarchy. It's, technically, an instance of multiple inheritance. Java doesn't generally allow this, but there's tons of special cases in the compiler for things that you can't do yourself. For example, defining operators is done in the compiler, but you can't define operators for your own classes.

It would be a bottom-type of reference-types. This wouldn’t work for value-types like int, at least not without boxing, which would be very painful.

Re: What if null was an Object in Java?

#50

Earlier quoted context omitted.

Yup. Your IDE will likely highlight it as an issue, but it's totally legal to return a null Optional. There's nothing special about it, it's just a wrapper class.

Did the project to add value types to Java (I’m sure I heard of it a decade ago) never finish?

https://openjdk.org/projects/valhalla
Post reply on HN