r/programming • • May 21 '20

Java programming language celebrates 25 years

https://www.infoworld.com/article/3544229/java-programming-language-celebrates-25-years.html
95 Upvotes

73 comments sorted by

View all comments

0

u/[deleted] May 21 '20

[deleted]

10

u/DoListening2 May 21 '20

Have you seen this talk? https://youtu.be/te3OU9fxC8U

They have a lot of stuff planned. Although they are heavily constrained in what they can/cannot do by backwards compatibility.

2

u/nutrecht May 22 '20

Unfortunately the main killer feature, null-safety, is not planned because there's no good way to do that without breaking compatibility.

1

u/DoListening2 May 22 '20

It is possible to do a gradual migration, where you would start with a compiler flag for your own code and libraries that also have it set (you can also safely use libraries that have @NonNull annotations, etc.), with the rest being treated similarly to Kotlin's platform types - no strict checks, but use at your own risk.

Eventually, most libraries would adopt this, or at least add the annotations. Way down the line, that compiler flag would be made the default.

I think Dart is currently undergoing a similar transition.

1

u/Yithar May 22 '20

https://www.reddit.com/r/java/comments/ee4h1x/null_safety/

However, seeing how Kotlin sometimes has uncanny long chains of "?." calls (e.g. Streams), I'm not convinced Kotlin has a completely superior solution. Especially since the Optional API is quite powerful in expressing missing values and arguably more readable than plain null checks.

It seems though there are some frameworks that already read the @NonNull annotation for null safety:

Check NullAway and checker-framework (which can offer you a lot more)


And even more powerful than Optional is Option. I personally think Option is really readable with map and flatMap but that's just my 2 cents.

1

u/DoListening2 May 22 '20 edited May 22 '20

The problem with Optional is that you still have to deal with regular null in the language. There is still a lot of code that returns/expects regular nulls. It may still seep in via uninitialized fields, etc. Without near-universal coverage, Optional's utility is greatly diminished, because you just can't rely on everything that is not Optional to also be non-null.

I also don't see any problem with ?.. If the only argument is that it "looks ugly" to some people, that's completely subjective and a matter of habit. I personally don't see value.map(o -> o.field).orElse(defaultValue) as more readable than value?.field ?: defaultValue at all. Plus you can use inline return syntax (val a = value?.field ?: return false, etc.).

I mean, the dot operator is the way of doing field/method access, and ?. is just a natural extension of that. I would guess that the vast majority of code inside lambdas passed to Optional.map is just plain field/method access and nothing else. Calculating other stuff in there surely must be much more rare (Kotlin's equivalent would be ?.let { }, which is definitely used less often than ?.field access). So turning plain dot-operator field/method access into a lambda inside map definitely feels more clunky to me.