I have a class with a private field that is a mutable collection. The field in this particular instance is an ArrayBuffer
, although my question extends to any finite, ordered, random-access collection type. I want to expose this field without permitting others to modify it. In Java I would add a method like:
private List<T> theList;
public List<T> getList() {
return Collections.unmodifiableList(theList);
}
In Java we just accept that the result is a List
that doesn't fully implement the List
interface because #add
and friends throw UnsupportedOperationException
.
In Scala, I would expect to find an appropriate trait with accessors like iterator
, size
, and apply
(for retrieving values by index) but no mutators. Does such a type exist that I just haven't found yet?