commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
Subject Re: [math] Read-only RealVector
Date Thu, 11 Aug 2011 11:22:36 GMT
Hello S├ębastien.

> >
> > Well, in fact I would very much like to have immutable vectors too.
> > Immutability is really a way to simplify implementations. Surprisingly it
> > sometimes also decrease time and memory consumption, because defensive
> > copies littering user code can be avoided.
> >
> Luc, I have a silly question. Why do you think immutable vectors would
> prevent defensive copy ? Creating an immutable vector from an existing
> mutable vector would require a defensive copy, wouldn't it?
> Thats the reason why I'm talking about read-only vectors, and not
> immutable vectors.
> The solver would keep a reference to the underlying (mutable) vector,
> so would be able to modify (indirectly) the read-only vector.
> The observer would only have a reference to the read-only vector, so
> would not be able to modify the underlying vector.

I think that you were right to stress the difference between immutable and
read-only.
If I'm not mistaken, in JDK parlance they call the latter "unmodifiable"
(cf. the methods "unmodifiableCollection" and siblings in the standard class
"java.util.Collections").

The idea which you referred to at the beginning of this thread is exactly
what they do: The wrapping class throws "UnsupportedOperationException" from
any "mutating" method.

What Luc said is that when you pass an (already) immutable object, you do not
have to make a copy; a reference is as safe.

I'm not sure whether that would still be true with an unmodifiable
reference. I.e. couldn't it be cast to its underlying modifiable reference?
However that would be on-purpose nasty code, not a mistake on the part of
the checker code.

So, a utility method like
  public static RealVector unmodifiableRealVector(RealVector v)
in class "AbstractRealVector", would fit your need, IMO.


Best,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message