J
John C. Bollinger
CLJA removed as not relevant to this branch of the discussion.
So as long as we're having this discussion, perhaps we can come up with
a real design for a better Java cloning mechanism. (Then we just have
to build a time machine, and this whole thread will vanish .) I
think, for those who are game, that we should start right at the
beginning, by defining the requirements and specifications. The
following are my own restatement of what I think are the key specs
behind Java's existing mechanism:
General Requirements
1) The cloning mechanism shall produce from an object a distinct object
of the same class that is logically a copy of the original.
2) The definition of "logically a copy" shall be controllable by
individual classes.
3) Classes shall be able to control whether or not their instances may
be cloned.
Specifics
4) A default cloning implementation shall be provided that performs a
field-by-field (shallow) copy of an object's state to initialize the clone.
Before we go on, then, the floor is open for discussion of the specs.
Does anyone know why the java designers didn't make every object
shallow-cloneable by default?
I understand the implications of cloning something overly hefty, and to be
cautious about such things, but it still seems a little overboard to me to
err on the side of turning if off.
Turning it off could easily have been accomplished by implementing something
called:
public interface NotCloneable {}
In fact, I might have allowed two methods, deep within Object:
public Object shallowClone()
and
public Object deepClone()
Now deep cloning is of course fraught with peril, and I am normally very
much in favor of the safety restrictions in the java language, but I'm not
sure this is the kind of thing that java should protect against.
So as long as we're having this discussion, perhaps we can come up with
a real design for a better Java cloning mechanism. (Then we just have
to build a time machine, and this whole thread will vanish .) I
think, for those who are game, that we should start right at the
beginning, by defining the requirements and specifications. The
following are my own restatement of what I think are the key specs
behind Java's existing mechanism:
General Requirements
1) The cloning mechanism shall produce from an object a distinct object
of the same class that is logically a copy of the original.
2) The definition of "logically a copy" shall be controllable by
individual classes.
3) Classes shall be able to control whether or not their instances may
be cloned.
Specifics
4) A default cloning implementation shall be provided that performs a
field-by-field (shallow) copy of an object's state to initialize the clone.
Before we go on, then, the floor is open for discussion of the specs.