253 lines
15 KiB
HTML
253 lines
15 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Issue DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES Writeup</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="../Issues/iss110_w.htm">
|
|
<LINK REL=UP HREF="../Issues/iss111.htm">
|
|
<LINK REL=NEXT HREF="../Issues/iss112_w.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="../Issues/iss110_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss111.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss112_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
|
|
|
|
<H2>Issue DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES Writeup</H2>
|
|
|
|
<PRE><B>Forum:</B> Cleanup<P>
|
|
<B>Issue:</B> <A HREF="iss111.htm">DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES</A><P>
|
|
<B>References:</B> DEFSTRUCT; CLtL p. 309<P>
|
|
Issue <A HREF="iss109.htm">DEFSTRUCT-CONSTRUCTOR-KEY-MIXTURE</A> (passed)<P>
|
|
<B>Category:</B> CLARIFICATION, CHANGE<P>
|
|
<B>Edit History:</B> V1, 11 Oct 1989, Sandra Loosemore<P>
|
|
V2, 02 Nov 1989, Sandra Loosemore (update discussion)<P>
|
|
<P>
|
|
<P>
|
|
<B>Problem Description:<P>
|
|
</B><P>
|
|
Must the symbols which name <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> slots be bound as <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A><P>
|
|
variables by the default keyword constructor function? Normally it<P>
|
|
would not matter, but if any of these symbols have been proclaimed<P>
|
|
<A REL=DEFINITION HREF="../Body/d_specia.htm#special"><B>SPECIAL</B></A> it will affect the dynamic environment in which the slot init<P>
|
|
forms are evaluated.<P>
|
|
<P>
|
|
There are three proposals, BOUND, NOT-BOUND, and VISIBLY-BOUND.<P>
|
|
<P>
|
|
<P>
|
|
<B>Background:<P>
|
|
</B><P>
|
|
CLtL requires each default slot init form to be evaluated "in the<P>
|
|
lexical environment of the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form in which it appeared". This<P>
|
|
means that the obvious technique of supplying the init forms as the<P>
|
|
defaults for the keyword arguments in the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> list of the<P>
|
|
constructor function is incorrect, unless care is taken to avoid<P>
|
|
shadowing any variable bindings of the symbols which correspond to<P>
|
|
those arguments.<P>
|
|
<P>
|
|
For example, given<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>defstruct</B></A> foo<P>
|
|
(a <a-init>)<P>
|
|
(b <b-init>)<P>
|
|
(c <c-init>))<P>
|
|
<P>
|
|
Generating the constructor as<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>defun</B></A> make-foo (<A REL=DEFINITION HREF="../Body/03_da.htm#AMkey"><B>&key</B></A> (a <a-init>) (b <b-init>) (c <c-init>)) ...)<P>
|
|
may not evaluate <b-init> and <c-init> in the correct lexical environment<P>
|
|
as specified in CLtL. Proposal VISIBLY-BOUND would change the specification<P>
|
|
to make this the correct behavior.<P>
|
|
<P>
|
|
One alternative is to wrap the init forms in closures named with gensyms:<P>
|
|
(<A REL=DEFINITION HREF="../Body/s_flet_.htm#flet"><B>flet</B></A> ((#:g1 () <a-init>)<P>
|
|
(#:g2 () <b-init>)<P>
|
|
(#:g3 () <c-init>))<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>defun</B></A> make-foo (<A REL=DEFINITION HREF="../Body/03_da.htm#AMkey"><B>&key</B></A> (a (#:g1)) (b (#:g2)) (c (#:g3))) ...))<P>
|
|
Under proposal BOUND, this would be the correct way to implement the<P>
|
|
constructor function.<P>
|
|
<P>
|
|
Another alternative is to make the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> variables themselves gensyms:<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>defun</B></A> make-foo (<A REL=DEFINITION HREF="../Body/03_da.htm#AMkey"><B>&key</B></A> ((:a #:g4) <a-init>)<P>
|
|
((:b #:g5) <b-init>)<P>
|
|
((:c #:g6) <c-init>)) ...)<P>
|
|
Under proposal NOT-BOUND, this would be the correct way to implement the<P>
|
|
constructor function.<P>
|
|
<P>
|
|
(Of course, it's possible that <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> could produce a simplified<P>
|
|
expansion in many cases by examining <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> init forms and/or lexical<P>
|
|
environment.)<P>
|
|
<P>
|
|
Issue <A HREF="iss109.htm">DEFSTRUCT-CONSTRUCTOR-KEY-MIXTURE</A> implies that BOA constructors<P>
|
|
do bind the symbols which name slots as <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> variables, since these<P>
|
|
variables can be referenced in the init forms for subsequent<P>
|
|
arguments.<P>
|
|
<P>
|
|
<P>
|
|
<B>Proposal (DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:BOUND):<P>
|
|
</B><P>
|
|
Clarify that the symbols which name slots must be bound as <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A><P>
|
|
variables by the keyword constructor function, in the order in which<P>
|
|
the slots are specified in the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form. Variables for<P>
|
|
inherited slots are bound before variables for explitly specified<P>
|
|
slots, in the order in which they were specified in the definition of<P>
|
|
the inherited <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>. Special bindings of these variables will be<P>
|
|
visible during the evaluation of the default init forms for subsequent<P>
|
|
slots. The slot default init forms are still evaluated in the <P>
|
|
lexical environment in which the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form itself appears.<P>
|
|
<P>
|
|
Rationale:<P>
|
|
<P>
|
|
This appears to be closest to the intent of CLtL.<P>
|
|
<P>
|
|
<P>
|
|
<B>Proposal (DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:NOT-BOUND):<P>
|
|
</B><P>
|
|
Clarify that the symbols which name slots must *not* be bound as<P>
|
|
<A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> variables by the keyword constructor function. The slot<P>
|
|
default init forms are evaluated in the lexical environment<P>
|
|
in which the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form itself appears and the dynamic environment<P>
|
|
in which the call to the constructor function appears.<P>
|
|
<P>
|
|
Rationale:<P>
|
|
<P>
|
|
This avoids the overhead of creating and invoking closures to compute<P>
|
|
the default values of the slots for the default keyword constructor.<P>
|
|
<P>
|
|
<P>
|
|
<B>Proposal (DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:VISIBLY-BOUND):<P>
|
|
</B><P>
|
|
Clarify that the symbols which name slots must be bound as <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A><P>
|
|
variables by the keyword constructor function, in the order in which<P>
|
|
the slots are specified in the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form. Variables for<P>
|
|
inherited slots are bound before variables for explitly specified<P>
|
|
slots, in the order in which they were specified in the definition of<P>
|
|
the inherited <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>. Special bindings of these variables will be<P>
|
|
visible during the evaluation of the default init forms for subsequent<P>
|
|
slots. <P>
|
|
<P>
|
|
Remove the requirement that the slot default init forms be evaluated<P>
|
|
in the lexical environment in which the <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> form itself appears.<P>
|
|
Instead, <A REL=DEFINITION HREF="../Body/f_provid.htm#require"><B>require</B></A> that they be evaluated in a lexical environment that<P>
|
|
contains bindings for the previous <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>lambda</B></A> variables of the constructor<P>
|
|
function. This applies to both the default keyword constructor function<P>
|
|
and BOA constructors.<P>
|
|
<P>
|
|
Rationale:<P>
|
|
<P>
|
|
This alternative corresponds most closely to current practice. It<P>
|
|
avoids the overhead of creating and invoking closures to compute the<P>
|
|
default values of the slots for both the default keyword constructor<P>
|
|
and BOA constructors.<P>
|
|
<P>
|
|
<P>
|
|
<B>Example/Test Cases:<P>
|
|
</B><P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>defvar</B></A> x 'global-x)<P>
|
|
(<A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>let</B></A> ((y 'local-y))<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>defstruct</B></A> baz (x 'x-init) (y x) (z y)))<P>
|
|
(make-baz)<P>
|
|
<P>
|
|
Under proposal BOUND,<P>
|
|
slot X is initialized to X-INIT<P>
|
|
slot Y is initialized to X-INIT<P>
|
|
(since <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> init form X is evaluated in <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> dynamic environment<P>
|
|
containing <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> binding to X-INIT)<P>
|
|
slot Z is initialized to LOCAL-Y<P>
|
|
(since <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> init form Y is evaluated in <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> lexical environment in<P>
|
|
which <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> appears)<P>
|
|
<P>
|
|
Under proposal NOT-BOUND,<P>
|
|
slot X is initialized to X-INIT<P>
|
|
slot Y is initialized to GLOBAL-X<P>
|
|
(since <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> constructor does <A REL=DEFINITION HREF="../Body/a_not.htm#not"><B>not</B></A> rebind <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> <A REL=DEFINITION HREF="../Body/d_specia.htm#special"><B>special</B></A> <A REL=DEFINITION HREF="../Body/f_docume.htm#variable"><B>variable</B></A> X)<P>
|
|
slot Z is initialized to LOCAL-Y<P>
|
|
<P>
|
|
Under proposal VISIBLY-BOUND,<P>
|
|
slot X is initialized to X-INIT<P>
|
|
slot Y is initialized to X-INIT<P>
|
|
(since <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> <A REL=DEFINITION HREF="../Body/d_specia.htm#special"><B>special</B></A> binding of X made by <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> constructor is visible)<P>
|
|
slot Z is initialized to X-INIT<P>
|
|
(since <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> lexical binding of Y made by <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> constructor is visible)<P>
|
|
<P>
|
|
<P>
|
|
<B>Current Practice:<P>
|
|
</B><P>
|
|
Most implementations (including Lucid CL, HPCL-I, KCL, CMU Common Lisp)<P>
|
|
appear to implement proposal VISIBLY-BOUND even though it is in conflict<P>
|
|
with what is required by CLtL.<P>
|
|
<P>
|
|
Utah Common Lisp currently implements proposal NOT-BOUND.<P>
|
|
<P>
|
|
<P>
|
|
<B>Cost to implementors:<P>
|
|
</B><P>
|
|
For proposal BOUND, the cost of implementing the proposal correctly is<P>
|
|
fairly small. The cost of implementing it both correctly and efficiently<P>
|
|
is potentially much larger.<P>
|
|
<P>
|
|
For proposal NOT-BOUND, the implementation cost is again fairly small, <P>
|
|
but it still requires essentially the same work as in proposal BOUND to<P>
|
|
handle BOA constructors correctly.<P>
|
|
<P>
|
|
Proposal VISIBLY-BOUND has the least implementation cost, since this<P>
|
|
is what most implementations already do and is the least complicated<P>
|
|
of the alternatives.<P>
|
|
<P>
|
|
<P>
|
|
<B>Cost to users:<P>
|
|
</B><P>
|
|
Adopting any of these proposals would improve the situation faced by<P>
|
|
users now.<P>
|
|
<P>
|
|
Users may find proposal VISIBLY-BOUND to be marginally more useful<P>
|
|
than the other alternatives since it allows the values of slots to be<P>
|
|
referenced in the subsequent default init forms.<P>
|
|
<P>
|
|
<P>
|
|
<B>Benefits:<P>
|
|
</B><P>
|
|
An area of confusion in the language is removed.<P>
|
|
<P>
|
|
<P>
|
|
<B>Discussion:<P>
|
|
</B><P>
|
|
Loosemore doesn't care much about which of these alternatives we<P>
|
|
adopt, but thinks that leaving this unspecified would be a mistake.<P>
|
|
<P>
|
|
Margolin says:<P>
|
|
By the way, I prefer this proposal [NOT-BOUND]. I think lexical<P>
|
|
environments should be captured (I think we've fixed everything so<P>
|
|
that init-forms are always evaluated in the appropriate lexical<P>
|
|
environment), but I don't like making the order of slots significant<P>
|
|
by allowing init forms to reference other slots. Order of slots is<P>
|
|
often constrained by other requirements (such as the :INCLUDE<P>
|
|
hierarchy, or using :TYPE to match a pre-existing <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>), so they<P>
|
|
shouldn't have an effect on the semantics of the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>.<P>
|
|
<P>
|
|
Moon says:<P>
|
|
Surely the default constructor function and "BOA constructors" must work<P>
|
|
compatibly. Unspecified initforms for optional and keyword arguments in<P>
|
|
a "BOA constructor" default from the slot initform rather than <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>, so<P>
|
|
"BOA constructors" face the same issue as default constructors.<P>
|
|
<P>
|
|
VISIBLY-BOUND seems semantically wrong.<P>
|
|
<P>
|
|
I would go with BOUND, assuming we can't just get rid of <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A><P>
|
|
entirely. I don't care about the supposed efficiency issue, which is<P>
|
|
easily gotten around or ignored.<P>
|
|
<P>
|
|
-------<P>
|
|
<P>
|
|
</PRE>
|
|
<HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|