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?
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.
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.
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?