r/programming • • Jan 16 '20

Records Come to Java

https://blogs.oracle.com/javamagazine/records-come-to-java
59 Upvotes

65 comments sorted by

View all comments

49

u/lunchlady55 Jan 17 '20

I can't wait til 2030 when cassettes come to Java and 2040 when compact disks come to Java.

24

u/sabellito Jan 17 '20

Can you imagine the hype in 3020 when they remove nulls.

13

u/lunchlady55 Jan 17 '20

But think of the horror of that decade when everything starts failing with "NullNotFound" errors...

6

u/eliasv Jan 17 '20 edited Jan 17 '20

There are ways to provide non-nullability in a backwards compatible way. You have to mark uses of a type as non-nullable. But obviously it'd be ugly and shitty to have to explicitly mark all uses of a type with something that should be default, so instead they could mark it at the import location. IDEs would be expected to import non-null versions by default.

import java.lang.Whatever!; // ! makes non-nullable

// uses of Whatever now are non-nullable.
// uses of Whatever! are non-nullable (but redundant)
// uses of Whatever? are nullable.

That covers language support, and they have a type operator proposal which could support this at the JVM level.

Edit: added a little more to illustrate how this would work in conjunction with use-site decoration.

3

u/nutrecht Jan 17 '20

That won't work. THere's tons of places where you have Strings that can be null alongside the ones that can't.

It's rather unlikely Java will adopt a null-safe typesystem like ever. If you want that, you're better off with Kotlin.

3

u/pron98 Jan 17 '20 edited Jan 17 '20

It's rather unlikely Java will adopt a null-safe typesystem like ever. If you want that, you're better off with Kotlin.

Doing that in Java, in a backward-compatible way, is about as hard as doing it in Kotlin and C#, and easier than adding generics (especially after generics have already been added). I don't know if Java will ever do this and how (there is some data-gathering going on ATM), but Java has made bigger changes before. As always, it's a matter of cost/benefit of this feature by itself, and this feature compared to others. I agree it's probably not a good idea to do this per type, but you could say that in a particular Java file, all references are non-nullable by default.

1

u/eliasv Jan 17 '20

You would still have the use-site decoration option I mentioned as a backup. The import-site decoration was just suggested as a trick to allow people to "change the default behaviour" without breaking backwards compatibility.

So in one file:

import java.lang.Whatever!; // ! means non-nullable

// uses of Whatever are non-nullable.
// uses of Whatever? are nullable.
// uses of Whatever! are non-nullable.

And in another file:

import java.lang.Whatever?; // ? means nullable (same as default)

// uses of Whatever are nullable.
// uses of Whatever? are nullable.
// uses of Whatever! are non-nullable.

2

u/nutrecht Jan 17 '20

That means I have to go look at the imports to see if String is nullable or non-nullable in every source file. Sorry to be a negative nancy, but I'd hate this.

1

u/eliasv Jan 17 '20

Fine with me! I'm not proposing a language feature, I have no attachment to the idea and no horse in the race. Just pointing out that it is possible, and imo relatively straightforward so far as these things go. Yes there are downsides though!

0

u/delrindude Jan 17 '20

I am forever awaiting for @deprectated on the null value