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

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

Re: Error: Symbol's value as variable is void


From: Denis Bueno
Subject: Re: Error: Symbol's value as variable is void
Date: Fri, 25 Mar 2005 14:26:59 -0500

On Fri, 25 Mar 2005 12:28:38 -0600, Joe Corneli
<jcorneli@math.utexas.edu> wrote:
> 
> I wrote some code that contains the following forms:
> 
> (defvar nero-link-regexp "\\[\\([0-9]+\\)\\]"
>   "Regular expression that tells nero what links look like.
> The first parenthesized subexpression is the unique string
> denoting the webpage to load, which will sought among the
> references.")

I'm guessing that if you wrap the above defvar in an
(eval-when-compile ...) and leave the other eval-when-compile present,
all will be fine.

> 
> (defvar nero-font-lock-keywords
>  (eval-when-compile
>   (list `(,nero-link-regexp . font-lock-keyword-face)))
>   "Font lock for `nero-mode'.
> Currently, only numbered links are fontified.")
> 
> These work fine for me, but another person using the code reports the
> following error upon byte-compiling:
> 
> Compiling file /stor/garray/src/nero.el at Fri Mar 25 08:21:48 2005
> nero.el:295:33:Error: Symbol's value as variable is void: nero-link-regexp
> 
> I don't see any such error when I byte compile.  I'm just curious to
> know if using `eval-when-compile' here is bad form, or what.  I think
> I've seen it being used in other packages in a similar context, but I
> don't understand it well enough to know whether I should be using it
> here.
> 
> Removing it did make the other user's error go away.  I told him to
> create a bug report, but if its just the fault of my bad code, I'd
> like to know.  More generally, as a point of style, when is it good to
> use `eval-when-compile'

Seems silly, but I think the answer is "whenever you need to evaluate
something at compile time". I once needed to use a similar form
(albeit I was programming in Common Lisp at the time, but
nevertheless), because I needed to evaluate a constant at
compile-time. In emacs lisp, the code would have looked something like
this:

(defvar c1 1)
(defvar c2 2)
(defvar c3 3)

(defun foo (x ...)
  (case x
     ((eval-when-compile c1) (do-something))
     ((eval-when-compile c2) (do-something-else))))

The reason I need the eval-when-compile was that `case' by default
doesn't evaluate its cases; so if eval-when-compile weren't there,
`case' would compare the value of x to the symbol c1, then c2, etc.

Usu. the case described above doesn't come up because you do things like:

(case x
  (3 (do-something))
  (4 (do-something-else)))

i.e., you have literal values as your cases. I didn't in my case.

(Note: The `case' form might work differently in emacs lisp; but
that's the way it works in common lisp. I'm just trying to give an
example of what I think is a proper usage of `eval-when-compile'.)

(Just to avoid befuddlement, the common lisp code looked like:

(defconstant c1 1)
(defconstant c2 2)

(defun foo (x ...)
  (case x
    (#.c1 (do-something))
    (#.c2 (do-something-else))))

In CL #. is a read-macro which evaluates its argument at read-time.)

Hope I haven't confused you. =]


-- 
Denis Bueno
PGP: http://pgp.mit.edu:11371/pks/lookup?search=0xA1B51B4B&op=index




reply via email to

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