1
0
Fork 0
cl-sites/novaspec.org/cl/f_with-slots.html

682 lines
12 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>with-slots | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_with-slots.html"
><LINK REL="next" HREF="f_defclass.html" TYPE="text/html" TITLE="defclass"
><LINK REL="prev" HREF="f_with-accessors.html" TYPE="text/html" TITLE="with-accessors"
><LINK REL="up" HREF="7_7_Objects_Dictionary.html" TYPE="text/html" TITLE="7.7 Objects Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="7_Objects.html"
>7. Objects</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="7_7_Objects_Dictionary.html"
>7.7 Objects Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_with-slots.html"
>with-slots</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_with-accessors.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_defclass.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="with-slots"
></SPAN
><B
>with-slots</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Macro</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>with-slots</B
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>slot-entry</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*)</SPAN
> <SPAN CLASS="cmssi"
>instance-form</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>declaration</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>form</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>result</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
></DIV
></DIV
><TABLE CELLSPACING="0" CELLPADDING="0" RULES="GROUPS" STYLE="margin: 1ex 0"
><TBODY
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;padding-right:3px"
><VAR CLASS="param"
>slot-entry</VAR
><SPAN CLASS="cmssi"
>::</SPAN
><SPAN CLASS="cmr"
>=</SPAN
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmssi"
>slot-name</SPAN
> <SPAN CLASS="cmsy"
>|</SPAN
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmssi"
>variable-name</SPAN
> <SPAN CLASS="cmssi"
>slot-name</SPAN
><SPAN CLASS="cmr"
>)</SPAN
></TD
></TR
></TBODY
></TABLE
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>slot-name</VAR
> &#8212; a <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>variable-name</VAR
> &#8212; a <EM CLASS="term"
>variable name</EM
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>instance-form</VAR
> &#8212; a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>; evaluted to produce <VAR CLASS="param"
>instance</VAR
>. </P
><P CLASS="j"
><VAR CLASS="param"
>instance</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>declaration</VAR
> &#8212; a <A HREF="m_declare.html" CLASS="misc"
><B
>declare</B
></A
> <A HREF="26_1_Glossary.html#expression"
><EM CLASS="term"
>expression</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>forms</VAR
> &#8212; an <A HREF="26_1_Glossary.html#implicit_progn"
><EM CLASS="term"
>implicit progn</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>results</VAR
> &#8212; the <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> returned by the <VAR CLASS="param"
>forms</VAR
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
>The macro <A HREF="f_with-slots.html" CLASS="macref"
><B
>with-slots</B
></A
> <A HREF="26_1_Glossary.html#establish"
><EM CLASS="term"
>establishes</EM
></A
> a <A HREF="26_1_Glossary.html#lexical_environment"
><EM CLASS="term"
>lexical environment</EM
></A
> for referring to the <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> in the <VAR CLASS="param"
>instance</VAR
> named by the given <VAR CLASS="param"
>slot-names</VAR
> as though they were <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variables</EM
></A
>. Within such a context the value of the <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> can be specified by using its slot name, as if it were a lexically bound variable. Both <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> and <A HREF="f_setq.html" CLASS="specref"
><B
>setq</B
></A
> can be used to set the value of the <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
>. </P
><P CLASS="j"
>The macro <A HREF="f_with-slots.html" CLASS="macref"
><B
>with-slots</B
></A
> translates an appearance of the slot name as a <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> into a call to <A HREF="f_slot-value.html" CLASS="funref"
><B
>slot-value</B
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (defclass thing ()
((x :initarg :x :accessor thing-x)
(y :initarg :y :accessor thing-y)))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #&lt;STANDARD-CLASS THING 250020173&gt;
(defmethod (setf thing-x) :before (new-x (thing thing))
(format t "~&amp;Changing X from ~D to ~D in ~S.~%"
(thing-x thing) new-x thing))
(setq thing (make-instance 'thing :x 0 :y 1)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #&lt;THING 62310540&gt;
(with-slots (x y) thing (incf x) (incf y)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(values (thing-x thing) (thing-y thing)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1, 2
(setq thing1 (make-instance 'thing :x 1 :y 2)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #&lt;THING 43135676&gt;
(setq thing2 (make-instance 'thing :x 7 :y 8)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #&lt;THING 43147374&gt;
(with-slots ((x1 x) (y1 y))
thing1
(with-slots ((x2 x) (y2 y))
thing2
(list (list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2))
(setq x1 (+ y1 x2))
(list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2))
(setf (thing-x thing2) (list x1))
(list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2)))))
<SPAN CLASS="cmr"
>&#8883;</SPAN
> Changing X from 7 to (9) in #&lt;THING 43147374&gt;.
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ((1 1 2 2 7 7 8 8)
9
(9 9 2 2 7 7 8 8)
(9)
(9 9 2 2 (9) (9) 8 8))</PRE
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
><A HREF="f_defclass.html" CLASS="macref"
><B
>defclass</B
></A
></P
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>The consequences are undefined if any <VAR CLASS="param"
>slot-name</VAR
> is not the name of a <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> in the <VAR CLASS="param"
>instance</VAR
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_with-accessors.html" CLASS="macref"
><B
>with-accessors</B
></A
>, <A HREF="f_slot-value.html" CLASS="funref"
><B
>slot-value</B
></A
>, <A HREF="f_symbol-macrolet.html" CLASS="specref"
><B
>symbol-macrolet</B
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>A <A HREF="f_with-slots.html" CLASS="macref"
><B
>with-slots</B
></A
> expression of the form:</P
><DIV CLASS="displaymath"
><SPAN CLASS="cmtt"
>(with-slots</SPAN
> <SPAN CLASS="cmr"
>(</SPAN
><I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><I
>n</I
></SUB
><SPAN CLASS="cmr"
>)</SPAN
> <I CLASS="i"
><I
>instance-form</I
></I
> <I CLASS="i"
><I
>form</I
></I
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I CLASS="i"
><I
>form</I
></I
><SUB CLASS="sub"
><I
>k</I
></SUB
><SPAN CLASS="cmr"
>)</SPAN
></DIV
><P CLASS="j"
>expands into the equivalent of</P
><DIV CLASS="displaymath"
><SPAN CLASS="cmtt"
>(let</SPAN
> <SPAN CLASS="cmtt"
>((</SPAN
><I
>in</I
> <I CLASS="i"
><I
>instance-form</I
></I
><SPAN CLASS="cmtt"
>))</SPAN
> <SPAN CLASS="cmtt"
>(symbol-macrolet</SPAN
> <SPAN CLASS="cmtt"
>(</SPAN
><I CLASS="i"
><I
>Q</I
></I
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I CLASS="i"
><I
>Q</I
></I
><SUB CLASS="sub"
><I
>n</I
></SUB
><SPAN CLASS="cmtt"
>)</SPAN
> <I CLASS="i"
><I
>form</I
></I
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>&#8201;<I CLASS="i"
><I
>form</I
></I
><SUB CLASS="sub"
><I
>k</I
></SUB
><SPAN CLASS="cmtt"
>))</SPAN
></DIV
><P CLASS="j"
>where <I CLASS="i"
><I
>Q</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> is</P
><DIV CLASS="displaymath"
><SPAN CLASS="cmtt"
>(</SPAN
><I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> <SPAN CLASS="cmr"
>()</SPAN
> <SPAN CLASS="cmtt"
>(slot-value</SPAN
> <I
>in</I
> <SPAN CLASS="cmr"
>&#8217;</SPAN
><I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
><SPAN CLASS="cmtt"
>))</SPAN
></DIV
><P CLASS="j"
>if <I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> is a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> and is</P
><DIV CLASS="displaymath"
><SPAN CLASS="cmtt"
>(</SPAN
><I CLASS="i"
><I
>variable-name</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> <SPAN CLASS="cmr"
>()</SPAN
> <SPAN CLASS="cmtt"
>(slot-value</SPAN
> <I
>in</I
> <SPAN CLASS="cmr"
>&#8217;</SPAN
><I CLASS="i"
><I
>slot-name</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
><SPAN CLASS="cmtt"
>))</SPAN
></DIV
><P CLASS="j"
>if <I CLASS="i"
><I
>slot-entry</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> is of the form</P
><DIV CLASS="displaymath"
><SPAN CLASS="cmtt"
>(</SPAN
><I CLASS="i"
><I
>variable-name</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
> <I CLASS="i"
><I
>slot-name</I
></I
><SUB CLASS="sub"
><I
>i</I
></SUB
><SPAN CLASS="cmtt"
>)</SPAN
></DIV
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_with-accessors.html" CLASS="prev"
>&#8592;</A
><A HREF="f_defclass.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>