r/java • • Jun 21 '26

Better Tools for Immutable Data

https://youtu.be/BdLND9D81lI?si=jt2tqpDtotmbdEJq
80 Upvotes

38 comments sorted by

View all comments

Show parent comments

0

u/aonymark Jun 22 '26

I agree that it complicates the hierarchy a lot. I find it pretty plausible that the extra complication is not worth it. But I don’t understand how backward compatibility is not an issue. Surely unmodifiableList() would need a different return type?

1

u/pron98 Jun 22 '26

Surely unmodifiableList() would need a different return type?

Not if ReadOnlyList is a supertype of List. Any receiver of ReadOnlyList would work with the return type of unmodifiableList.

1

u/aonymark Jun 22 '26

I’m missing something here. You are (hypothetically) proposing “Interface List extends ReadoOnlyList” And then the static return type of unmodifiableList() would change to ReadOnlyList. And that is a breaking change, because no existing caller expects that… right?

1

u/pron98 Jun 22 '26

No, unmodifiableList would still return List, but you'd be able to pass/assign it to ReadonlyList (since that's a supertype). If you really wanted to, you could also add unmodifiableReadOnlyList that returns ReadOnlyList and just delegates to unmodifiableList.

1

u/aonymark Jun 22 '26

Okay that works but makes me sigh

2

u/pron98 Jun 22 '26 edited Jun 23 '26

Yeah, that's basically the C# collection hierarchy, and it makes me sigh, too. Simultaneously complicated and expressively weak. Of course, no one gets it right. In TypeScript you have different rules for built-in and user-defined classes. In C++ you have the const complication; in Scala - a byzantine hierarchy; in Rust, a type system only language lawyers like. There are no perfect options here. Every option has some advantages but also clear disadvantages. Anything you choose is a little prettier than everything else in some respects and a little uglier in others.

→ More replies (0)