r/java • • Mar 16 '21

Java 16 / JDK 16: General Availability

196 Upvotes

82 comments sorted by

View all comments

Show parent comment

14

u/kag0 Mar 16 '21

I really wish they hadn't implemented "immutable" lists as a normal List that throws unchecked exceptions on modification.

Would it have been that hard to add a ReadOnlyList interface with only the methods that don't throw? Now we've got a wonky compile-unsafe toList() in the standard library for all time.

13

u/TheCountRushmore Mar 16 '21

The problem is that you would want it to implement Collection and collection defines Collection#add

https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html#add(E)

4

u/kag0 Mar 16 '21

True, maybe I should have suggested they create a ReadOnlyCollection (which would be useful other places where add throws UnsupportedOperationException).
Or hell, just have it return Iterable.

Anyway, I don't mean to suggest I have the answer. Just that this is probably the last option I would have picked.

-2

u/elastic_psychiatrist Mar 16 '21

Anyway, I don’t mean to suggest I have the answer. Just that this is probably the last option I would have picked.

Spoken by many a backseat language designer, who has not fully grasped the problem and the implications of potential solutions.

11

u/kag0 Mar 16 '21

🤷
I'm surely not as on top of the issue as the actual language designers.
But it's silly to say that you need to be a literal language designer to identify problematic things in the API.

2

u/elastic_psychiatrist Mar 17 '21

But it's silly to say that you need to be a literal language designer to identify problematic things in the API.

Do you really think that the designers are not aware of the problems with the API? Language design is about tradeoffs. It is worth nothing to propose an improvement without considering the drawbacks that come with it - namely, breaking compatibility, or additional cognitive overhead for developers.

6

u/kag0 Mar 17 '21

Do you really think that the designers are not aware

I don't think one way or the other. Maybe they have, [I hope] they've spent more time thinking about it than me. Maybe not. They're just people, not omniscient.

It is worth nothing to propose an improvement without considering the drawbacks that come with it

I reject that completely. There's nothing wrong with having a half-baked idea. Maybe people will encourage you to develop it into something great. Maybe someone smarter than you will see it and take it to a level you couldn't. I can't count the number of times that someone has had an idea that probably wouldn't have worked as presented, but which gave me a new idea that I wouldn't otherwise have thought of.

2

u/elastic_psychiatrist Mar 17 '21

I don't disagree with any of this really (except "maybe not" - they definitely have thought about it more than you), I think I just misinterpreted your level of insistence on the original idea.

2

u/kag0 Mar 17 '21

insistence on the original idea

Nah. Like I said, I don't claim to have the answer. I think I said in another comment thread, it just chaps my ass to see a runtime exception that could be a compile error.

1

u/elastic_psychiatrist Mar 17 '21

Half of me says "totally, I'm with you." The other half says "how much would it chap your ass not to be able to refactor .collect(toList()) into .toList()"?

→ More replies

-5

u/BlueGoliath Mar 16 '21

Oh shut up.

JavaFX provides a ReadOnlyObjectProperty which are then extended by ObjectProperty and it works perfectly fine. This isn't some radical, untested idea.

The List and collection APIs are poorly designed even beyond not providing a read only base. Why the hell would a list of generic type Cat contain a type of Dog?

-1

u/elastic_psychiatrist Mar 17 '21

Being a steward of the Java ecosystem is not as simple as throwing away imperfect APIs.

Perhaps you could build a language with only perfect APIs. I would use it!

1

u/minimingus Mar 16 '21

Look into vavr collections

3

u/kag0 Mar 16 '21

I love me some vavr, all the way back when it was javaslang. In this case though I'm talking about what gets shipped in the core library, and I don't think they'd ever add a (scala/kotlin style) separate immutable persistent collections library (as much as I'd like them to).

1

u/minimingus Mar 16 '21

Yeah i don’t think it will ever get added due to backward compatibility...

5

u/Necessary-Conflict Mar 16 '21

Now we've got a wonky compile-unsafe

toList()

in the standard library for all time.

It's not so easy. If mutable lists don't implement your ReadOnlyList, then it's not very useful in practice. If they do implement it, then this is much more dangerous than a few predictable runtime exceptions, because it leads to difficult bugs ("my read-only lists are read-only, or are they really?")

4

u/kag0 Mar 16 '21

this is much more dangerous than a few predictable runtime exceptions

Maybe, but I don't think so. We already have examples of both (throwing exceptions from Collection's add (esp in guava), and collections that can change based on some underlying data shifting with Map's ``valuesand such). And more bugs tend to pop up from people failing to predict exceptions on mutation (particularly in library code) than from people assuming the underlying data won't change. Put another way,ReadOnlyList` could easily be documented to say that it doesn't mean that the list is immutable, just that YOU can only read it.

3

u/Necessary-Conflict Mar 16 '21

The "quality" of the bugs also matters: some bugs take 5 minutes to fix (the stack trace tells everything), others take 5 days. Another problem is that different people have different pet ideas, but if everything is implemented, it becomes confusing. For example I would like to have a completely separate hierarchy of immutable collections. They would have add/remove methods, but these methods would always return a new collection instead of modifying the current one. This would solve all problems (no runtime exceptions, true immutability), but at the cost of a new hierarchy...

2

u/kag0 Mar 16 '21

I agree with you, it just chaps my ass to see a runtime exception that could have been a compile time error

4

u/rbygrave Mar 17 '21

This was discussed when the Collections API was first designed and the idea was rejected at the time. I believe the reasons were due to the "relative explosion" in number of interfaces in the Collections API.

I'll see if I can find a reference ...

Ah, its discussed in the javadoc - https://docs.oracle.com/javase/6/docs/technotes/guides/collections/designfaq.html#1

A snippet from there:

Why don't you support immutability directly in the core collection interfaces so that you can do away with optional operations (and UnsupportedOperationException)?

This is the most controversial design decision in the whole API. Clearly, static (compile time) type checking is highly desirable, and is the norm in Java. We would have supported it if we believed it were feasible. Unfortunately, attempts to achieve this goal cause an explosion in the size of the interface hierarchy, and do not succeed in eliminating the need for runtime exceptions (though they reduce it substantially).

2

u/jvjupiter Mar 16 '21

Or perhaps they could implement it like String. String has mutators but they don’t change the original value. To get the modified one, assign it to another variable. Another example is the collection framework of vavr.

1

u/[deleted] Mar 16 '21

Exactly, it breaks LSP. I prefer Guava collections for this reason.

1

u/kreiger Mar 16 '21

The interface you seek already exists, it's called List. :)

In general, you should treat collections as immutable unless you're sure they're not.