123 lines
7.4 KiB
HTML
123 lines
7.4 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: Macro WITH-SLOTS</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="m_w_acce.htm">
|
|
<LINK REL=UP HREF="c_object.htm">
|
|
<LINK REL=NEXT HREF="m_defcla.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="m_w_acce.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_object.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_defcla.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="with-slots"><I>Macro</I> <B>WITH-SLOTS</B></A> <P>
|
|
<P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>with-slots</B> <I>(<I>slot-entry</I><B>*</B>) instance-form <I>declaration</I><B>*</B> <I>form</I><B>*</B></I><P> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<PRE>
|
|
slot-entry::= slot-name | (variable-name slot-name)
|
|
</PRE>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>slot-name</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>; not evaluated. <P>
|
|
<I>variable-name</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A>; not evaluated. <P>
|
|
<I>instance-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluted to produce <I>instance</I>. <P>
|
|
<I>instance</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
The macro <A REL=DEFINITION HREF="#with-slots"><B>with-slots</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#establish"><I>establishes</I></A> a <A REL=DEFINITION HREF="26_glo_l.htm#lexical_environment"><I>lexical environment</I></A> for referring to the <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slots</I></A> in the <I>instance</I> named by the given <I>slot-names</I> as though they were <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variables</I></A>. Within such a context the value of the <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> can be specified by using its slot name, as if it were a lexically bound variable. Both <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> and <A REL=DEFINITION HREF="s_setq.htm#setq"><B>setq</B></A> can be used to set the value of the <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A>. <P>
|
|
The macro <A REL=DEFINITION HREF="#with-slots"><B>with-slots</B></A> translates an appearance of the slot name as a <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> into a call to <A REL=DEFINITION HREF="f_slt_va.htm#slot-value"><B>slot-value</B></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(defclass thing ()
|
|
((x :initarg :x :accessor thing-x)
|
|
(y :initarg :y :accessor thing-y)))
|
|
=> #<STANDARD-CLASS THING 250020173>
|
|
(defmethod (setf thing-x) :before (new-x (thing thing))
|
|
(format t "~&Changing X from ~D to ~D in ~S.~%"
|
|
(thing-x thing) new-x thing))
|
|
(setq thing (make-instance 'thing :x 0 :y 1)) => #<THING 62310540>
|
|
(with-slots (x y) thing (incf x) (incf y)) => 2
|
|
(values (thing-x thing) (thing-y thing)) => 1, 2
|
|
(setq thing1 (make-instance 'thing :x 1 :y 2)) => #<THING 43135676>
|
|
(setq thing2 (make-instance 'thing :x 7 :y 8)) => #<THING 43147374>
|
|
(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)))))
|
|
>> Changing X from 7 to (9) in #<THING 43147374>.
|
|
=> ((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>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A> <P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
The consequences are undefined if any <I>slot-name</I> is not the name of a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> in the <I>instance</I>. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_w_acce.htm#with-accessors"><B>with-accessors</B></A>, <A REL=DEFINITION HREF="f_slt_va.htm#slot-value"><B>slot-value</B></A>, <A REL=DEFINITION HREF="s_symbol.htm#symbol-macrolet"><B>symbol-macrolet</B></A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
A <A REL=DEFINITION HREF="#with-slots"><B>with-slots</B></A> expression of the form: <P>
|
|
(with-slots (<I>slot-entry</I>1<I></I> ... <I>slot-entry</I>n<I></I>) <I>instance-form</I> <I>form</I>1<I></I> ... <I>form</I>k<I></I>) <P>
|
|
expands into the equivalent of <P>
|
|
|
|
<PRE>
|
|
(let ((in instance-form))
|
|
(symbol-macrolet (Q1 ... Qn) form1 ... formk))
|
|
</PRE>
|
|
</TT> <P>
|
|
where <I>Q</I>i is <P>
|
|
|
|
<PRE>
|
|
(slot-entryi () (slot-value in 'slot-entryi))
|
|
</PRE>
|
|
</TT> <P>
|
|
if <I>slot-entry</I>i is a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> and is <P>
|
|
|
|
<PRE>
|
|
(variable-namei () (slot-value in 'slot-namei))
|
|
</PRE>
|
|
</TT> <P>
|
|
if <I>slot-entry</I>i is of the form <P>
|
|
<PRE>
|
|
(variable-namei 'slot-namei))
|
|
</PRE>
|
|
</TT> <P>
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss097.htm">DECLS-AND-DOC</A><P></UL><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>
|