interface method with readonly (const) parameters, not final

H

HK

What I would like to do is this:

public interface Boo {
Boo mergeWith(Boo const other) const;
}

The intention would be to force an implementation
to not modify any of `this' or `other', but return either
one unchanged or create a freshly allocated object.

I read
http://groups-beta.google.com/group...3de?tvc=2&q=readonly+method+parameters+final+
and
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070

but most of the discussion and proposed workarounds
relate to concrete implemented methods. Any ideas of how to
come at least close to the above other than by calling
the method only ever with copies of the original
things or write blurb in *BOLD* into the spec?

Thanks,
Harald.
 
B

Boudewijn Dijkstra

HK said:
What I would like to do is this:

public interface Boo {
Boo mergeWith(Boo const other) const;
}

The intention would be to force an implementation
to not modify any of `this' or `other', but return either
one unchanged or create a freshly allocated object.

How about an ImmutableBoo and a MutableBoo?
 
H

HK

Boudewijn said:
How about an ImmutableBoo and a MutableBoo?

Well, if you could explain how this would solve
the problem. I don't see it. In particular, mergeWith
will always be able to change (mess up) `this', which
is exactly what I would like to prevent.

Harald.
 
J

John C. Bollinger

HK said:
Well, if you could explain how this would solve
the problem. I don't see it. In particular, mergeWith
will always be able to change (mess up) `this', which
is exactly what I would like to prevent.

You cannot prevent it by means of the method declaration. You also
cannot use the method declaration to prevent implementations from
invoking accessible methods on their reference-type arguments. You can
prevent direct manipulation of your objects' fields by making the fields
private. If you need to control the implementation then you need to
_provide_ the implementation. You can strongly influence the
implementation, however, by your definition of the Boo interface. If it
provides no means to mutate a Boo, then no correct implementation of the
mergeWith(Boo) method can modify its argument. If an implementation is
in fact truly immutable, then it cannot modify itself, either.
 

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top