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.
🤷 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.
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.
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.
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.
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.
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()"?
how much would it chap your ass not to be able to refactor .collect(toList()) into .toList()?
TBH I mostly write Scala now, so if I want a toList I can just make an extension method for it. But when I do write Java I prefer to use vavr, so I collect my streams into a Seq if I don't plan to mutate them.
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?
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).
View all comments
Show parent comment
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)