<br><br><div class="gmail_quote">On Mon, Sep 6, 2010 at 12:59 AM, Scott L. Burson <span dir="ltr"><<a href="mailto:Scott@sympoiesis.com">Scott@sympoiesis.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Sun, Sep 5, 2010 at 11:56 AM, Yakov Zaytsev <span dir="ltr"><<a href="mailto:zaytsev.yakov@gmail.com" target="_blank">zaytsev.yakov@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
<br><div><div>(defmacro define-struct-getter (struct package &rest slots)</div><div> (loop for s in slots</div><div> do (let ((acc (intern (concatenate 'string (symbol-name struct) "-" (symbol-name s))))</div>
<div> (sp (intern s package)))</div><div> (eval `(defmacro ,acc (object)</div><div> `(access-slot ,object ',',sp)))))) <br></div></div></blockquote><blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
<div> <br></div></blockquote></div><br></div>Rather than eval-ing the consed defmacro form at macroexpansion time, which doesn't do the right thing when a DEFINE-STRUCT-GETTER form is in a file being compiled, it would be better to do something like this:<div class="im">
<br>
<br>(defmacro define-struct-getter (struct package &rest slots)<br></div> `(progn<br> ,@(mapcar (lambda (s)<div class="im"><br> (let ((acc (intern (concatenate 'string (symbol-name struct) "-" (symbol-name s))))<br>
(sp (intern s package))<br></div><div class="im"> `(defmacro ,acc (object)<br> `(access-slot object ',',sp))))<br></div> slots)))<br><br>As tempting as it is to call EVAL for various purposes, the reality is that correct uses of EVAL are quite rare. In fact, unless you're writing your own read-eval-print loop of some kind, the best rule of thumb is that if you're calling EVAL explicitly, you've made a mistake. I have trouble coming up with any exceptions to this rule other than a REPL.<br>
<br>Also, supplying a package to re-intern the slot names in, while not outright wrong, is an unusual kind of thing to do. Is it that hard to just type the slot names with package prefixes?<br><br>All that said -- if you're using nested backquotes successfully, you're clearly starting to get the hang of macros.<br>
<font color="#888888">
<br>-- Scott<br><br>
</font><br></blockquote><div><br></div><div>re-interning the slot names saves typing because DEFINE-STRUCT-GETTERS form is used for structures which are generated by swig. anyhow it is first time I use INTERN</div><div>e.g.</div>
<div><br></div><div>(define-struct-getter image :MagickWand columns rows)</div><div><br></div><div>and thank you for pointing my EVAL usage mistake!</div></div>