www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion for an Old-keyword

reply Kari Salminen <Kari_member pathlink.com> writes:
Hi,
I thought I'd bring up a suggestion for an Old-keyword (The idea taken
straight from the Eiffel language):

- An old-keyword:

Some webpages for info on the old-keyword:
http://omicron.felk.cvut.cz/FAQ/articles/a511.html and under that
"LOLD: What does the 'old' keyword mean?".

http://www.comp.ufla.br/~monserrat/eiffel/advanced_introduction/eiffel.html#s7.2.2.1

Old-keyword would be useful for use in postconditions.

Here's some info on the Old-expression that I typed in from the
the pages 123-125 of the book "Eiffel The Language" by Bertrand Meyer
(ISBN 0-13-247925-7):

Old expression

A special form of expression, the Old expression, is available in routine
postconditions only.

In a postcondition clause, the expression old exp has the same type as exp; its
value at execution time, on routine exit, is the value of exp as evaluated on
routine entry in the current call.

An example appeared in the postcondition for forth above. Here is another
from routine put in class FIXED_QUEUE of the Data Structure Library; the
routine inserts an element into a queue:

put(V: T) is
-- Add item v to queue.
require
not_full: not full
do
..
ensure
count = old count + 1;
(old empty) implies (item = v);
not empty;
array_item((last-1+capacity) \\ capacity)=v
end -- put

The first postcondition clause indicates that any call to put increases the
queue size, count, by one. The second indicates that if the queue was initially
empty (as expressed by old empty), the the value at cursor position (as given by
function item) will be the one just inserted. Operator implies is boolean
implication.
The syntax of an Old expression is simply

Old (Symbol = with a triangle on it) old Expression

In expressions involving other symbols, the precedence of the old symbol is
higher than that of any operator. So the second Assertion_clause of put above
could have been written as

old empty implies item=v

the other parentheses being also optional since = (equality) has a higher
precedence than implies (implication). Using extra parentheses does not hurt, of
course, and may enhance readability.
The validity constraint expresses that a Postcondition is the only permitted
context for an Old expression:

An Old expression of the form old e, where e is an expression of type TE, is
valid if and only if it satisfies the following two conditions:
1) It appears in a Postcondition clause of a Routine r.
2) Transforming r into a function with result type TE (by adding a result
type if r is procedure, or changing its result type it is already a
function) and replacing its entire Routine part by
do
Result := e
end
would yield a valid routine.

The second condition indicates that e must be an expression that would be
valid in a "do" or "once" body for r, but does not involve any local entities. A
more indirect formulation is necessary because r does necessarily have such a
body (it could be of the External or Deferred form). The device of making r a
function provides us with a suitable syntactical stage for e's guest apperance
in
the simplified routine.
The value of an Old expression old e is defined only at the end of the
execution of a call to r, just before the call returns; it is the result that
would
have been produced by evaluating e just before the call's execution began.

------------------------------------------

Well, what do you think? Would it be useful in the D language?

---
Kari Salminen
kaasalm NOSPAM at utu dot fi
To email me take out the spaces, NOSPAM and replace at with ' ' and dot with '.'
Oct 06 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <ck1j0e$3mm$1 digitaldaemon.com>, Kari Salminen says...
Hi,
I thought I'd bring up a suggestion for an Old-keyword (The idea taken
straight from the Eiffel language):
..
Well, what do you think? Would it be useful in the D language?
Yes. It would be nice to have the initial values of function parameters available in the out block, though I'm not sure I'd want to take the performance hit if this weren't done intelligently. Also, what if the parameter is a class type? Should the compiler be expected to do a deep copy or will only the reference be the same? Sean
Oct 06 2004
parent Kari Salminen <Kari_member pathlink.com> writes:
In article <ck1jn7$49m$1 digitaldaemon.com>, Sean Kelly says...
In article <ck1j0e$3mm$1 digitaldaemon.com>, Kari Salminen says...
I thought I'd bring up a suggestion for an Old-keyword
Well, what do you think? Would it be useful in the D language?
Yes. It would be nice to have the initial values of function parameters available in the out block, though I'm not sure I'd want to take the performance hit if this weren't done intelligently. Also, what if the parameter is a class type? Should the compiler be expected to do a deep copy or will only the reference be the same?
I don't know about the performance problems. They're up to the ones who implement the compiler (Of course hopefully there'll be no problems :)). You got me thinking about that deep copy/shallow copy/reference-thing, so let's take an example and let's also assume obj is a reference to an object: a) Old (obj.func) b) (Old obj).func If I understood correctly how the Old-keyword was specified in the Eiffel The Language -book I think that a) should give the value of the obj.func on entry to the function in whose out-block we are writing the a)-clause. And b) should give the value of the func-function in the out-block but using the reference ("pointer") obj on entry to the function in whose out-block we are writing the b)-clause. So I think a) would be about identical to: Before a function's in-part we make a local variable and initialize it using this: old_objfunc = obj.func; And in the out-block we would write old_objfunc; And b) would be about identical to: Before a function's in-part we make a local variable and initialize it using this: old_obj = obj; And in the out-block we would write old_obj.func; So only the reference would be copied here, not the referenced object. Hopefully some of my thoughts can be deciphered from that text above ;-). Also I shamelessly copied the following from the webpage http://omicron.felk.cvut.cz/FAQ/articles/a511.html LOLD: What does the 'old' keyword mean? "The value of an Old expression old e is [...] the result that would have been produced by evaluation e just before the call's execution began." (ETL2, p.125). This is useful in postconditions. When using the keyword with a reference, it is clear from the definition that the value of "old a" will be the object to which 'a' referred at the beginning of the routine, and not the old value of the actual object. Obtaining a copy of 'a', if that is the required semantics, has to be done explicitly: .. old (clone (a)) ... This makes senses because copy and identity are issues with multiple solutions -- a shallow copy as done by 'clone' by default may not be enough for instance -- and a compiler cannot be reasonably expected to guess correctly which interpretation is appropriate for a given usage. --- Kari Salminen kaasalm NOSPAM at utu dot fi To email me take out the spaces, NOSPAM and replace at with ' ' and dot with '.'
Oct 07 2004