Le 06/11/2020 à 19:00, Alan Snyder a écrit :
(…snip…) But a question that deserves ongoing review is whether Java should support immutable collections using a separate type hierarchy (…snip…).

Maybe an alternative way (admittedly more difficult) to have immutable collections without introducing new interfaces could be to take inspiration from the C++ "const" keyword? Especially since in Java, "const" is already a reserved keyword, just not used yet.

I realize that it would be a difficult task: how to handle private fields that are just caches in a const object; where to add the "const" keyword in existing JDK API; how to make such change in a backward compatible way (e.g. when a legacy code overrides a "const" method). I do not have any expectation. But given the inconvenient of alternatives, I wonder if there is some research about the long-term feasibility of a "const" semantic in Java? If it was the case, then maybe it would be better to not add immutable collection interfaces in Java for now.

    Regards

        Martin


Reply via email to