unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* reference variable before definition
@ 2017-03-12  2:03 Eric Abrahamsen
  2017-03-12  2:06 ` Eric Abrahamsen
  2017-03-12  3:18 ` Stefan Monnier
  0 siblings, 2 replies; 6+ messages in thread
From: Eric Abrahamsen @ 2017-03-12  2:03 UTC (permalink / raw)
  To: help-gnu-emacs

I can't tell if it's always been this way: I'm getting compiler warnings
about "reference to free variable" if the variable appears in a function
definition before it's defined in the file. So compiling this produces
warnings:

(defun make-my-day ()
  (message "It's: %s" my-day))

(defvar my-day "Monday")

Has it always been this way, and it only just got through to me? I
didn't think it was necessary to define everything before use in a
function...

Hoping I'm just hallucinating something,
Eric





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: reference variable before definition
  2017-03-12  2:03 reference variable before definition Eric Abrahamsen
@ 2017-03-12  2:06 ` Eric Abrahamsen
  2017-03-12  3:18 ` Stefan Monnier
  1 sibling, 0 replies; 6+ messages in thread
From: Eric Abrahamsen @ 2017-03-12  2:06 UTC (permalink / raw)
  To: help-gnu-emacs

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

[...]

> Hoping I'm just hallucinating something,

It just occurred to me, I might be seeing the difference between "B" in
dired and `batch-byte-compile' on the command line...




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: reference variable before definition
  2017-03-12  2:03 reference variable before definition Eric Abrahamsen
  2017-03-12  2:06 ` Eric Abrahamsen
@ 2017-03-12  3:18 ` Stefan Monnier
  2017-03-12  3:40   ` Eric Abrahamsen
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2017-03-12  3:18 UTC (permalink / raw)
  To: help-gnu-emacs

> Has it always been this way, and it only just got through to me? I
> didn't think it was necessary to define everything before use in a
> function...

I think it's been that way for a while, yes.  It used not to be terribly
important, indeed, but since Emacs-24 it became a lot more important,
because the byte-compiler compiles variables differently if they've been
declared via `defvar`.  And it doesn't first scan the buffer for
defvars, so by the time it sees the defvar it has already (mis)compiled
the earlier uses of that variable.


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: reference variable before definition
  2017-03-12  3:18 ` Stefan Monnier
@ 2017-03-12  3:40   ` Eric Abrahamsen
  2017-03-12  3:55     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Abrahamsen @ 2017-03-12  3:40 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Has it always been this way, and it only just got through to me? I
>> didn't think it was necessary to define everything before use in a
>> function...
>
> I think it's been that way for a while, yes.  It used not to be terribly
> important, indeed, but since Emacs-24 it became a lot more important,
> because the byte-compiler compiles variables differently if they've been
> declared via `defvar`.  And it doesn't first scan the buffer for
> defvars, so by the time it sees the defvar it has already (mis)compiled
> the earlier uses of that variable.

Okay. Apparently this also applies to defclasses and slot access -- the
classes need to be defined before any function calls slot-value. I'm
surprised I never ran into this before...

Off to churn some code,
Eric




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: reference variable before definition
  2017-03-12  3:40   ` Eric Abrahamsen
@ 2017-03-12  3:55     ` Stefan Monnier
  2017-03-12  7:26       ` Eric Abrahamsen
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2017-03-12  3:55 UTC (permalink / raw)
  To: help-gnu-emacs

> Okay. Apparently this also applies to defclasses and slot access -- the
> classes need to be defined before any function calls slot-value.  I'm
> surprised I never ran into this before...

This one *is* new, tho.  And it's really just a warning to help catch
typos (and cases where init-args are misused for slot names): it doesn't
have any actual effect.


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: reference variable before definition
  2017-03-12  3:55     ` Stefan Monnier
@ 2017-03-12  7:26       ` Eric Abrahamsen
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Abrahamsen @ 2017-03-12  7:26 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Okay. Apparently this also applies to defclasses and slot access -- the
>> classes need to be defined before any function calls slot-value.  I'm
>> surprised I never ran into this before...
>
> This one *is* new, tho.  And it's really just a warning to help catch
> typos (and cases where init-args are misused for slot names): it doesn't
> have any actual effect.

Good to know. But some of us are compulsive about hygiene!




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-03-12  7:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-12  2:03 reference variable before definition Eric Abrahamsen
2017-03-12  2:06 ` Eric Abrahamsen
2017-03-12  3:18 ` Stefan Monnier
2017-03-12  3:40   ` Eric Abrahamsen
2017-03-12  3:55     ` Stefan Monnier
2017-03-12  7:26       ` Eric Abrahamsen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).