[Prev][Next][Index][Thread]

Re: A Proposal!!



In article <8g9s98$uco$1@nnrp1.deja.com>, Jon Hewitt
<hewittj1752@my-deja.com> wrote:

> One of the gotchas that has bothered me about Dylan is add!().  In
> particular I based my code on the Harlequin's Task List Manager example,
> it compiled but it didn't work.  The problem turned out to be that I was
> using the <list> class for my collection class instead of the
> <stretchy-vector> class that the example used.  Thus since the result of
> add!(<list>, <object>) is not necessarily == to the passed in <list>,
> one must utilize the result.  The Task List Manager code uses an
> optimization: since the result is guaranteed to be == the supplied
> <stretchy-vector> it could skip assigning the result to a local
> variable.

Then that's a bug in their code, unless it was absolutely certain that it
would only be applied to a stretchy-vector.

Methods with a ! such as add! are *allowed* to return one of their
arguments, destructuvely modified, but there is nothing that says that
they have to, and especially you aren't allowed to assume that they do. 
Basically the purpose of having add!() as well as add() is that add!() is
allowed to do whatever it wants and in particular whatever is most
efficient.

>From the DRM (/drm_102.htm#HEADING102-264):

-------------------------------------------
Returns a sequence that contains new-element and all the elements of
source-sequence. The result-sequence may or may not be freshly allocated.
It may share structure with a preexisting sequence. source-sequence and
result-sequence may or may not be ==.

       source-sequence may be modified by this operation.

       result-sequence's size is one greater than the size of
source-sequence. The generic function add! doesn't specify where the new
element will be added, although individual methods may do so.
-------------------------------------------

And a little further down:

-------------------------------------------
The result of add! on a stretchy vector is == to the stretchy-vector
argument, which is modified by this operation. add!
-------------------------------------------

Is it really necessary to specify this in the language spec?  It
constrains implementations and it's unsafe to assume this if it turns out
not to be true.  If the programmer writes "a := add!(a, foo)" and the
result is equal to the old value then 1) it's not *that* inefficient, and
2) a good compiler will figure this out anyway (the common case code is
likely to be inlined).


-- Bruce



Follow-Ups: References: