Call by reference in Dylan 
Author Message
 Call by reference in Dylan

[The meaning of ``call by value'' as it relates to Dylan, Scheme, CL,
etc, should probably be in a FAQ.]

Quote:

> Maybe someone out there can shed some light on this topic:

> Why doesn't Dylan support "call by reference" (similar to C++)?  Coming
> from a C/C++ background, such a feature would seem like it would be a
> standard thing to support in function calls.

> Isn't there extra overhead in creating a new instance of an object,
> copying the values of the original object to the new object when you
> "call by value"?  It seems that with call by reference, Dylan would only
> have to do type checking.

The semantics you're suggesting (``call by copy''?) are not what Dylan
does.

``Call by value'' means the caller passes a value to the callee.  In
Dylan's case, that value is an object, which can be side-effected by the
callee.  (Assignments to the variable named by the parameter inside the
callee have no effect in the caller.)

``Call by reference'' means the address of a variable is passed to the
callee, so the callee, by assigning to its parameter, can change what
object the variable is bound to.

These are, I think, pretty standard definitions for the terms.  Some
people disagree.  (I don't care.)  This definition of ``call by value''
corresponds exactly to what Dylan, Scheme, Java, Eiffel, etc, do,
regardless of what they call it.

Here's an example I posted yesterday.  Since Dylan uses call by value,
this code causes no change to *the-counter*:

  define variable *the-counter* = 0;

  define method increment (counter :: <integer>) => ()
    counter := counter + 1;  // insensible in Dylan
  end method increment;

  increment(*the-counter*);  // doesn't do anything

whereas this does increment a slot in *the-counter*:

  define class <counter> (<object>)
    slot value = 0;
  end class <counter>;

  define constant *the-counter* = make(<counter>);

  define method increment (counter :: <counter>) => ()
    counter.value := counter.value + 1;
  end method increment;

  increment(*the-counter*);

If Dylan had call by reference semantics, the result of the increment
in the first example would be that *the-counter* contains the value 1
afterwards.  That isn't what happens.

Quote:
> Does "call by reference" have other implications to program structure or
> something?

Call by reference, ala Pascal var parameters or C++ reference types,
whereby the binding of a local name to its value is affected by a
function call is, to some people, a bad thing.  Personally, I'm an
agnostic on the issue.  Dylan doesn't have it as part of the function
call protocol, but macros can have the same effect.

Quote:
> If Dylan doesn't support call by reference, how are destructive fuctions
> such as sort! implemented?   As macros?

They are functions.  They don't require anything special.  You write
them in the obvious way, and they work.

Paul



Sat, 18 Apr 1998 03:00:00 GMT  
 Call by reference in Dylan
I recall that fortran used "call by reference" and that
"call by value" was a main point of departure for Algol.

In Lisp, and successors to Algol such as C, "call by value"
means that a parameter can be modified without causing its
corresponding argument to change;  but composite entities
are normally passed as pointers to the memory they occupy.

Pure "call by value" would require explicit "call by reference"
overrides wherever side-effects were desired.  So a pointer is
passed, but a variable from which it comes can not be modified
unless a "locative" is [allowed to be] passed for the variable.

Macros aside, Common Lisp and Dylan make callers set their own
variables.



Tue, 28 Apr 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Simulating call-by-reference with Dylan macros

2. Simulating call-by-reference with Dylan macros

3. IV Called by reference doesn't open when called

4. Cross reference/Call structure tools for C function calls

5. Call by reference vs. call by value

6. Call by reference vs. call by value

7. Book FS: Dylan Reference Manual, by Andrew Shalit.

8. How update of Dylan reference manual?

9. How update of Dylan reference manual?

10. Dylan Reference Manual questions

11. Dylan Reference Manual and MacWorld

12. Dylan Reference Manual online now

 

 
Powered by phpBB® Forum Software