Newsgroups: comp.lang.dylan
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsfeed.cit.cornell.edu!newsstand.cit.cornell.edu!news.kei.com!news.mathworks.com!newsfeed.internetmci.com!howland.reston.ans.net!ix.netcom.com!netcom.com!netcom9.netcom.com!haahr
From: haahr@netcom.com (Paul Haahr)
Subject: Re: Call by reference in Dylan
In-Reply-To: phinely@Hawaii.Edu's message of Mon, 30 Oct 1995 17:32:02 GMT
To: phinely@Hawaii.Edu (Peter Hinely)
Message-ID: <HAAHR.95Oct31160842@netcom9.netcom.com>
Sender: haahr@netcom9.netcom.com
Organization: NETCOM On-line services
References: <DH9vDE.HDM@news.hawaii.edu>
Date: Tue, 31 Oct 1995 16:08:42 GMT
Lines: 77

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

Peter Hinely <phinely@Hawaii.Edu> wrote:
> 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.

> 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.

> 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
