help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Concern around use of eval


From: Tassilo Horn
Subject: Re: Concern around use of eval
Date: Thu, 19 Mar 2015 09:00:06 +0100
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Les Harris <les@lesharris.com> writes:

Hi Les,

> ,----
> | (defvar lh/labels '("label1" "label2" "label3"))
> | 
> | (defmacro lh/gen-predicate (label)
> |   `(defun ,(intern (concat "lh/" label "-p")) ()
> |             (member ,label *lh/system-label-store*)))
> | 
> | (defun lh/define-predicates ()
> |   (dolist (label lh/labels)
> |     (eval `(lh/gen-predicate ,label))))
> | (lh/define-predicates)
> `----
>
> Now this all works fine and I get my auto-generated predicates, so
> success. My question/concern/niggle is around the use of (eval) in
> lh/define-predicates. If I don't put eval in there then the defun the
> macro evaluates into never gets evaluated itself. Stylistically, is
> there a better way to do this or am I just being weird about (eval) and
> should just get over it?

Well, in case the `lh/label' value is available at compile-time then
`lh/define-predicates' can also be a macro and you can go like this:

--8<---------------cut here---------------start------------->8---
(defvar lh/labels '("label1" "label2" "label3"))

(defmacro lh/gen-predicate (label)
  `(defun ,(intern (concat "lh/" label "-p")) ()
     (member ,label *lh/system-label-store*)))

(defmacro lh/define-predicates ()
  `(progn
     ,@(mapcar (lambda (label)
                 `(lh/gen-predicate ,label))
               lh/labels)))

(lh/define-predicates)
--8<---------------cut here---------------end--------------->8---

But if you know your labels only at runtime, then I think there's no way
around using `eval'.

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]