Subject: Re: Implementing Closures
From: (Rob Warnock)
Date: 1999/09/06
Newsgroups: comp.lang.lisp
Message-ID: <7qvt82$>
Dobes Vandermeer  <> wrote:
| trying to find an efficient and elegant way of implementing enclosures
| ... Some broken solutions come easily to mind:
| 1. The closures store a pointer or a reference up the stack to the
| parent context's variable; this solves the problem of sharing the
| variable between closures and the parent context, but is broken because
| when the parent context is gone, the variables would not be valid.

Actually, this is not "broken", and works quite well as long as
the parent's context is *not* deallocated explicitly, but rather is
garbage-collected, in which case it will live just exactly as long
as it needs to (i.e., as long as there are any live references to it).

Start by thinking about *all* bound variables being heap-allocated
(and garbage-collected), and only *then* (after you see how that solves
your problem) consider possible ways to optimize it (e.g., perhaps by
stack-allocating variables that are never referenced by closures).

Read "Lisp In Small Pieces", by Christian Queinnec [Cambridge University
Press] (originally in French as "Les Langages Lisp", InterEditions, 1994),
which discusses such issues in great variety & depth...


Rob Warnock, 8L-846
Applied Networking
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		FAX: 650-933-0511
Mountain View, CA  94043	PP-ASEL-IA