110 lines
6.2 KiB
HTML
110 lines
6.2 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-ACCESSORS</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="f_mk_l_1.htm">
|
|
<LINK REL=UP HREF="c_object.htm">
|
|
<LINK REL=NEXT HREF="m_w_slts.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="f_mk_l_1.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_w_slts.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="with-accessors"><I>Macro</I> <B>WITH-ACCESSORS</B></A> <P>
|
|
<P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>with-accessors</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::= (variable-name accessor-name)
|
|
</PRE>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<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>accessor-name</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A>; not evaluated. <P>
|
|
<I>instance-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated. <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>
|
|
Creates a lexical environment in which the slots specified by <I>slot-entry</I> are lexically available through their accessors as if they were variables. The macro <A REL=DEFINITION HREF="#with-accessors"><B>with-accessors</B></A> invokes the appropriate accessors to <I>access</I> the <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slots</I></A> specified by <I>slot-entry</I>. 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>
|
|
<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 thing1 (make-instance 'thing :x 1 :y 2)) => #<THING 43135676>
|
|
(setq thing2 (make-instance 'thing :x 7 :y 8)) => #<THING 43147374>
|
|
(with-accessors ((x1 thing-x) (y1 thing-y))
|
|
thing1
|
|
(with-accessors ((x2 thing-x) (y2 thing-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 1 to 9 in #<THING 43135676>.
|
|
>> 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>accessor-name</I> is not the name of an accessor for the <I>instance</I>. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_w_slts.htm#with-slots"><B>with-slots</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-accessors"><B>with-accessors</B></A> expression of the form: <P>
|
|
<PRE>
|
|
(with-accessors (slot-entry1 ... slot-entryn) instance-form form1 ... formk)
|
|
</PRE>
|
|
</TT> <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>
|
|
(variable-namei () (accessor-namei in))
|
|
</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>
|