diff options
| author | Earl Hyatt | 2025-03-12 23:01:49 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2025-03-31 14:29:48 -0400 |
| commit | e04d1dafc700813c835ae4e45af4e104c49e8875 (patch) | |
| tree | 405296248ee9da13e065213d8f88cfe317a7bc81 /doc | |
| parent | a97a61b630624f5a6ec917db92e2985c56b20aa0 (diff) | |
| download | emacs-e04d1dafc700813c835ae4e45af4e104c49e8875.tar.gz emacs-e04d1dafc700813c835ae4e45af4e104c49e8875.zip | |
Add cl-with-accessors
* lisp/emacs-lisp/cl-macs.el (cl-with-accessors): New macro.
* doc/misc/cl.texi (Structures): Mention the new macro.
* test/lisp/emacs-lisp/cl-macs-tests.el (cl-lib-struct-with-accessors):
New Test.
* etc/NEWS (New macro 'cl-with-accessors'.): Mention the macro.
This macro is useful when making repeated use of a structures accessor
functions, such as reading from a slot and then writing to a slot. It
is similar to 'with-slots' from EIEIO, but uses accessor functions
instead of slot names.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/misc/cl.texi | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/doc/misc/cl.texi b/doc/misc/cl.texi index e51e245c736..7219494391b 100644 --- a/doc/misc/cl.texi +++ b/doc/misc/cl.texi | |||
| @@ -4066,6 +4066,55 @@ A documentation string describing the slot. | |||
| 4066 | 4066 | ||
| 4067 | Other slot options are currently ignored. | 4067 | Other slot options are currently ignored. |
| 4068 | 4068 | ||
| 4069 | @defmac cl-with-accessors name bindings body@dot{} | ||
| 4070 | You can use @code{cl-with-accessors} to lexically define symbols as | ||
| 4071 | expressions calling the given accessor functions on a single instance of | ||
| 4072 | a structure or class defined by @code{cl-defstruct} or @code{defclass} | ||
| 4073 | (@pxref{eieio}). This can simplify code that repeatedly accesses slots. | ||
| 4074 | With it, you can use @code{setf} and @code{setq} on the symbols like | ||
| 4075 | normal variables, modifying the values in the structure. Unlike the | ||
| 4076 | macro @code{with-slots} (@pxref{Accessing Slots,,,eieio,EIEIO}), because | ||
| 4077 | the symbol expands to a function call, @code{cl-with-accessors} can be | ||
| 4078 | used with any generalized variable that can take a single argument, such | ||
| 4079 | as @code{cl-first} and @code{cl-rest}. | ||
| 4080 | @end defmac | ||
| 4081 | |||
| 4082 | @example | ||
| 4083 | ;; Using accessors with long, clear names without the macro: | ||
| 4084 | (defun internal-normalization (person) | ||
| 4085 | "Correct the values of the slots in PERSON to be as expected." | ||
| 4086 | ;; Check the values of the structure: | ||
| 4087 | (when (equal (person-optional-secondary-data person) "") | ||
| 4088 | (setf (person-optional-secondary-data person) nil)) | ||
| 4089 | (when (null (person-access-settings person)) | ||
| 4090 | (setf (person-access-settings person) 'default)) | ||
| 4091 | (when (< (long-accessor-name-that-can-become-unreadable-when-repeated | ||
| 4092 | person) | ||
| 4093 | 9) | ||
| 4094 | (cl-incf (long-accessor-name-that-can-become-unreadable-when-repeated | ||
| 4095 | person) | ||
| 4096 | 100)) | ||
| 4097 | ;; And so on before returning the structure: | ||
| 4098 | person) | ||
| 4099 | |||
| 4100 | ;; Using accessors with long, clear names with the macro: | ||
| 4101 | (defun internal-normalization (person) | ||
| 4102 | "Correct the values of the slots in PERSON to be as expected." | ||
| 4103 | (cl-with-accessors ((secondary-data person-optional-secondary-data) | ||
| 4104 | (access-settings person-access-settings) | ||
| 4105 | (short-name person-much-longer-accessor-name)) | ||
| 4106 | person | ||
| 4107 | ;; Check the values of the structure: | ||
| 4108 | (when (equal secondary-data "") | ||
| 4109 | (setf secondary-data nil)) | ||
| 4110 | (when (null access-settings) | ||
| 4111 | (setf access-settings 'default)) | ||
| 4112 | (when (< short-name 9) | ||
| 4113 | (cl-incf short-name 100)) | ||
| 4114 | ;; And so on before returning the structure: | ||
| 4115 | person)) | ||
| 4116 | @end example | ||
| 4117 | |||
| 4069 | For obscure historical reasons, structure options take a different | 4118 | For obscure historical reasons, structure options take a different |
| 4070 | form than slot options. A structure option is either a keyword | 4119 | form than slot options. A structure option is either a keyword |
| 4071 | symbol, or a list beginning with a keyword symbol possibly followed | 4120 | symbol, or a list beginning with a keyword symbol possibly followed |