21 July 2008

Java wishlist - immutable arrays

Over the last x years I often found myself wishing for various features, thought I'd get them down in written form so I don't forget. As they come back to me I will jot them down.

An immutable array would throw an exception if anyone tried to change the reference at any one of its elements. This would allow one to return the array from a method invocation without having to defensively copy it. Of course, since Java 5, with generics, you can return typed lists that throw an error if modified.