Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!pasteur!helios.ee.lbl.gov!ucsd!ucsbcsl!eiffel!bertrand From: bertrand@eiffel.UUCP (Bertrand Meyer) Newsgroups: comp.lang.eiffel Subject: Re: Request for your favorite OOP idea for possible implementation! Keywords: OOP := clone copy deepCopy Message-ID: <146@eiffel.UUCP> Date: 1 Jun 89 04:14:54 GMT References: <3720@qut.edu.au> <859@clyde.Concordia.CA> Organization: Interactive Software Engineering, Santa Barbara CA Lines: 65 From <859@clyde.Concordia.CA>, marcap@concour.CS.Concordia.CA (Marc Pawlowsky): > 1) := is inconsistent. In both Eiffel and Smalltalk := when applied > to a primitive (integer, real, character, ...) automatically > creates a new object, with the same value. Yet when := is used > for a class it creates a reference to the same object. It would > be nice if this inconsistency was cleared up, by having := always > refer to the same object and not create a new one. What is important is to have an operation that always has the same semantics. In Eiffel this is called Clone and has the same semantics for simple values and class types. (Sometimes, rather than Clone, one may wish to use copy, introduced by version 2.2, which is the same as Clone except that no new allocation is entailed.) Contrary to what Mr. Pawlowsky seems to imply, an operation which has dual semantics, such as := in Eiffel (copy for simple values, reference for objects), is indeed useful. For example in most cases it provides what one wants as semantics for ``assignment to an element of an array'': a.enter (i, x) in Eiffel. If x is an integer, you want it to be copied to entry i of a; if it is a reference, you don't want the object to be copied. There are exceptions, of course, but this seems to be the general case. Given that we have both an operation which ensures copy semantics and one which ensures dual semantics, the question remains of whether it was a good idea to use the simpler symbol (:=) for the latter, reserving a more cumbersome one (Clone) for the former. I believe the right choice was made in Eiffel. If computer programming started in 1989 the answer might be different but given that hundreds of thousands of current programmers (and hence future Eiffel programmers) are used to a certain meaning for :=, reversing this meaning would have been irresponsible, potentially leading to massive errors in software produced in years to come. > 2) There is no true clone, deepCopy methods in either language. By this > I mean a method that recursively returns a new object, with all the > instances variables also cloned (deepCopy). This is often needed. While > it is possible to create such a method, I think it should be part of the > language definition. Eiffel version 2.2 has a deep copy and a deep equal operation, defined not as language primitives but as features in the library class ANY, inherited by everyone. (By the way, a forthcoming message will give the precise list of everything new in 2.2.) > A simple operator should be used (e.g. <-, or :-). This is purely a syntactical question. Because deep copy and deep equal may be defined as normal features in a library class, we felt there was there is no need to invent yet another special notation. In Eiffel, writing a.deep_copy (b) seems highly preferable (more clear to both a newcomer and an experienced programmer, easier to remember etc.) to something like a :- b. In fact, I believe the latter to be quite dangerous because of its similarity to a := b, which has a drastically different semantics. (See also section 5.8.3 in my book ``Object-Oriented Software Construction''.) -- -- Bertrand Meyer bertrand@eiffel.com