User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cl:macros:with-accessors [2019/08/16 12:00]
cl:macros:with-accessors [2019/08/26 03:00] (current)
Line 1: Line 1:
 +====== Macro WITH-ACCESSORS ======
 +
 +====Syntax====
 +  * with-accessors (//​slot-entry''​*''//​) //​instance-form//​ //​declaration''​*''//​ //​form''​*''//​ → //​result''​*''//​
 +
 +<​blockquote>​
 +slot-entry ::= (//​variable-name//​ //​accessor-name//​)
 +</​blockquote>​
 +
 +====Arguments and Values====
 +  * //​variable-name//​ - a //​[[CL:​Glossary:​variable name]]//; not evaluated.
 +  * //​accessor-name//​ - a //​[[CL:​Glossary:​function name]]//; not evaluated.
 +  * //​instance-form//​ - a //​[[CL:​Glossary:​form]]//;​ evaluated.
 +  * //​declaration//​ - a **[[CL:​Symbols:​declare]]** //​[[CL:​Glossary:​expression]]//;​ not evaluated.
 +  * //forms// - an //​[[CL:​Glossary:​implicit progn]]//.
 +  * //results// - the //​[[CL:​Glossary:​values]]//​ returned by the //forms//.
 +
 +====Description====
 +Creates a lexical environment in which the slots specified by //​slot-entry//​ are lexically available through their accessors as if they were variables. The macro **with-accessors** invokes the appropriate accessors to //access// the //​[[CL:​Glossary:​slot|slots]]//​ specified by //​slot-entry//​. Both **[[CL:​Macros:​setf]]** and **[[CL:​Special Operators:​setq]]** can be used to set the value of the //​[[CL:​Glossary:​slot]]//​.
 +
 +====Examples====
 +<​blockquote>​
 +([[CL:​Macros:​defclass]] thing () 
 +  ((x :initarg :x :accessor thing-x) ​
 +   (y :initarg :y :accessor thing-y))) ​
 +<​r>#<​STANDARD-CLASS THING 250020173>​ </r>
 +([[CL:​Macros:​defmethod]] ([[CL:​Macros:​setf]] thing-x) :before (new-x (thing thing)) ​
 +  ([[CL:​Functions:​format]] [[CL:​Constant Variables:​t]] "​~&​Changing X from ~D to ~D in ~S.~%"
 +          (thing-x thing) new-x thing))
 +<​r>#<​STANDARD-METHOD (SETF THING-X) :BEFORE (T THING) 28226194></​r>​
 +([[CL:​Macros:​defparameter]] *thing1* ([[CL:​Functions:​make-instance]] 'thing :x 1 :y 2)) <​r>#<​THING 43135676>​ </r>
 +([[CL:​Macros:​defparameter]] *thing2* ([[CL:​Functions:​make-instance]] 'thing :x 7 :y 8)) <​r>#<​THING 43147374>​ </r>
 +(with-accessors ((x1 thing-x) (y1 thing-y)) *thing1* ​
 +  (with-accessors ((x2 thing-x) (y2 thing-y)) *thing2* ​
 +    ([[CL:​Functions:​list]] ([[CL:​Functions:​list]] x1 (thing-x *thing1*) y1 (thing-y *thing1*) ​
 +                x2 (thing-x *thing2*) y2 (thing-y *thing2*)) ​
 +          ([[CL:​Macros:​setf]] x1 ([[CL:​Functions:​math-add|+]] y1 x2)) 
 +          ([[CL:​Functions:​list]] x1 (thing-x *thing1*) y1 (thing-y *thing1*) ​
 +                x2 (thing-x *thing2*) y2 (thing-y *thing2*)) ​
 +          ([[CL:​Macros:​setf]] (thing-x *thing2*) ([[CL:​Functions:​list]] x1)) 
 +          ([[CL:​Functions:​list]] x1 (thing-x *thing1*) y1 (thing-y *thing1*) ​
 +                x2 (thing-x *thing2*) y2 (thing-y *thing2*)))))
 +<​o>​Changing X from 1 to 9 in #<THING 43135676>​.
 +Changing X from 7 to (9) in #<THING 43147374>​. </o>
 +<​r>​((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))</​r>​
 +</​blockquote>​
 +
 +====Affected By====
 +**[[CL:​Macros:​defclass]]**
 +
 +====Exceptional Situations====
 +The consequences are undefined if any //​accessor-name//​ is not the name of an accessor for the //​instance//​.
 +
 +====See Also====
 +  * **[[CL:​Macros:​with-slots|Macro WITH-SLOTS]]**
 +  * **[[CL:​Special Operatoors:​symbol-macrolet|Special Operator SYMBOL-MACROLET]]**
 +
 +====Notes====
 +A **with-accessors** expression of the form:
 +
 +<​blockquote>​
 +(with-accessors (//​slot-entry<​sub>​1</​sub>//​ ... //​slot-entry<​sub>​n</​sub>//​) //​instance-form// ​
 +  //​form<​sub>​1</​sub>// ​
 +  ... 
 +  //​form<​sub>​k</​sub>//​)
 +</​blockquote>​
 +
 +expands into the equivalent of
 +
 +<​blockquote>​
 +([[CL:​Special Operators:​let]] ((in //​instance-form//​))
 +  ([[CL:​Special Operators:​symbol-macrolet]] (//​Q<​sub>​1</​sub>//​ ... //​Q<​sub>​n</​sub>//​) ​
 +    //​form<​sub>​1</​sub>// ​
 +    ... 
 +    //​form<​sub>​k</​sub>//​))
 +</​blockquote>​
 +
 +where Q<​sub>​i</​sub>​ is:
 +
 +<​blockquote>​
 + ​(//​variable-name<​sub>​i</​sub>//​ () (//​accessor-name<​sub>​i</​sub>//​ in))
 +</​blockquote>​
 +
 +\issue{DECLS-AND-DOC}