Re: Global lexical variables?



On 2008-10-30, Russell Wallace <russell.wallace@xxxxxxxxx> wrote:
On Oct 30, 8:42 pm, Kaz Kylheku <kkylh...@xxxxxxxxx> wrote:
The ``it'' that CLISP is not approving of here is that you are trying
to use an undefined variable.  This is not well-defined behavior in Lisp.

But I did define it:

(defvar clauses-table
(make-hash-table
:test 'equal))

Doesn't that count as a definition?

It does. I see now that it's only a compiler warning you are getting, while
compiling a particular function.

This means that the definition has not been seen at the time when the compiler
is processing the function.

If the definition is in another file, it means that the file hasn't been
loaded yet.

If the definition is in the same file, the warning means that it's later in the
file than the reference. Thus at the point of reference, the variable isn't
known yet.

The compiler is then only guessing that this free variable will later be
defined as special, and is compiling the reference that way.

A diagnostic is useful, because the compiler is guessing. Maybe you didn't
intend for the reference to be a special variable. Suppose you made this
mistake:

(defun my-func ()
foo)

(define-symbol-macro foo ...) ;; oops, later than reference

Right?

You will run into this error when you have a program made of several modules,
which define some special variables, and there is a circular dependency: module
A uses B's global variable, and B uses A's global variable.

In both possible orders, someone is compiled without the DEFVAR.

One way out of that is to put important global variables into a separate file
dedicated to global variables.

Also, DEFVAR with no value can be used as a forward reference in resolving
these situations. It will ensure that the variable exists, without changing its
value.
.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... Even if we grant that, there remain others which are not matters of opinion, ... and cannot use it as a *reference*. ... Failure to do so causes common bugs. ... I do compiler stuff; we don't actually do our own compiler/libc ...
    (comp.lang.c)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)
  • Re: Garbage Collection Eligibility and portability
    ... (in the parlance of compiler writers). ... There isn'tany way to get a given Lisp to act that way. ... that alters exactly when a variable reference to a memory location ceases holding it from collection. ... The other, which I've seen called "Aggressive garbage collection" in some articles on this behavior in C#/.Net, appears to make a memory location eligible the instant the last reference to the location is used. ...
    (comp.lang.lisp)
  • Re: Why we should (not?) have closures after all
    ... Programmers should have tools that can tell them as much as possible about their code's likely behavior. ... The wart can be used anywhere a type is used in declaring a reference ... The compiler does nothing special if a "can be null" reference is ... it would be nice if there was a variant of the "assert" statement that was always enabled and threw IllegalArgumentException instead of AssertionError. ...
    (comp.lang.java.programmer)
  • Re: Late Binding Question
    ... I still don't understand why the compiler thinks this is late bound?? ... I never did get what all the fuss about late binding is? ... "Microsoft.Office.Interop.Excel" reference in the Dot Net tab. ... Obviously the PIAs (primary interop assemblies) are ...
    (microsoft.public.dotnet.languages.vb)