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.

1 comment:

  1. I also want the same thing. I'm not a VM or compiler designer, but it seems like it would be pretty easy to implement and could have some memory / performance advantages, because I could allow classes to return references to arrays.

    ReplyDelete