unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* eval-when-load?
@ 2015-07-16 16:33 Stephen Leake
  2015-07-16 18:13 ` eval-when-load? Artur Malabarba
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Stephen Leake @ 2015-07-16 16:33 UTC (permalink / raw)
  To: emacs-devel

A project I'm working on has a form like this at top level in a file:

(defvar jde-jdhelper-singleton (jde-jdhelper nil)
  "The JDHelper singleton instance.")

`jde-jdhelper' can throw an error, which it does during byte-compile (it
complains about not finding "wget").

We want the variable defined at load time, and we want the error at load
time, but we don't care at compile time.

So I'd like to do:

(eval-when-load
 (defvar jde-jdhelper-singleton (jde-jdhelper nil)
   "The JDHelper singleton instance."))

but `eval-when-load' is not defined.

Is there a work-around?

We could change the var to a function that checks if the initialization
is done, but that seems a heavy solution. 

-- 
-- Stephe



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

* Re: eval-when-load?
  2015-07-16 16:33 eval-when-load? Stephen Leake
@ 2015-07-16 18:13 ` Artur Malabarba
  2015-07-16 22:04   ` eval-when-load? Stephen Leake
  2015-07-16 22:13 ` eval-when-load? Philipp Stephani
  2015-07-16 23:03 ` eval-when-load? Stefan Monnier
  2 siblings, 1 reply; 7+ messages in thread
From: Artur Malabarba @ 2015-07-16 18:13 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

Is jde-jdhelper a macro? If it's a function the behavior you're asking
about is the default.

For instance, if I add (defvar oi (mapcar 'car nil)) to a file and
compile it, the byte-compiled file will be identical.

2015-07-16 17:33 GMT+01:00 Stephen Leake <stephen_leake@stephe-leake.org>:
> A project I'm working on has a form like this at top level in a file:
>
> (defvar jde-jdhelper-singleton (jde-jdhelper nil)
>   "The JDHelper singleton instance.")
>
> `jde-jdhelper' can throw an error, which it does during byte-compile (it
> complains about not finding "wget").
>
> We want the variable defined at load time, and we want the error at load
> time, but we don't care at compile time.
>
> So I'd like to do:
>
> (eval-when-load
>  (defvar jde-jdhelper-singleton (jde-jdhelper nil)
>    "The JDHelper singleton instance."))
>
> but `eval-when-load' is not defined.
>
> Is there a work-around?
>
> We could change the var to a function that checks if the initialization
> is done, but that seems a heavy solution.
>
> --
> -- Stephe
>



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

* Re: eval-when-load?
  2015-07-16 18:13 ` eval-when-load? Artur Malabarba
@ 2015-07-16 22:04   ` Stephen Leake
  0 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2015-07-16 22:04 UTC (permalink / raw)
  To: emacs-devel

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> Is jde-jdhelper a macro? If it's a function the behavior you're asking
> about is the default.

jde-jdehelper is an eieio class:

(require 'eieio)

(defclass jde-jdhelper ()
 ...)
 

-- 
-- Stephe



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

* Re: eval-when-load?
  2015-07-16 16:33 eval-when-load? Stephen Leake
  2015-07-16 18:13 ` eval-when-load? Artur Malabarba
@ 2015-07-16 22:13 ` Philipp Stephani
  2015-07-16 23:03 ` eval-when-load? Stefan Monnier
  2 siblings, 0 replies; 7+ messages in thread
From: Philipp Stephani @ 2015-07-16 22:13 UTC (permalink / raw)
  To: Stephen Leake, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 309 bytes --]

Stephen Leake <stephen_leake@stephe-leake.org> schrieb am Do., 16. Juli
2015 um 18:34 Uhr:

> So I'd like to do:
>
> (eval-when-load
>  (defvar jde-jdhelper-singleton (jde-jdhelper nil)
>    "The JDHelper singleton instance."))
>
> but `eval-when-load' is not defined.
>

cl-lib has (cl-eval-when (load) ...)

[-- Attachment #2: Type: text/html, Size: 636 bytes --]

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

* Re: eval-when-load?
  2015-07-16 16:33 eval-when-load? Stephen Leake
  2015-07-16 18:13 ` eval-when-load? Artur Malabarba
  2015-07-16 22:13 ` eval-when-load? Philipp Stephani
@ 2015-07-16 23:03 ` Stefan Monnier
  2015-07-17 10:07   ` eval-when-load? Stephen Leake
  2 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2015-07-16 23:03 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

> A project I'm working on has a form like this at top level in a file:
> (defvar jde-jdhelper-singleton (jde-jdhelper nil)
>   "The JDHelper singleton instance.")
> `jde-jdhelper' can throw an error, which it does during byte-compile (it
> complains about not finding "wget").
> We want the variable defined at load time, and we want the error at load
> time, but we don't care at compile time.

I suspect there's a confusion about "compile time": the above call to
jde-jdhelper will not be executed while compiling the file that contains
this defvar.  It will only be called when the resulting .elc file is
loaded (so, as Artur says, what you ask is already the default
behavior).

But I suspect the problem you see is that the above form appears in
a file which is not only byte-compiled but is also `require'd by other
files in that package, so when *those* files are byte-compiled, the call
to jde-jdhelper is indeed executed, since at that time this file is
loaded (as part of byte-compiling those other files).

Maybe you want to use

   (if t (require 'this-file))

instead of (require 'this-file) so that this-file is only loaded when
those-files are *loaded* rather than when they're byte-compiled.

Of course, the same problem can happen if those-files are themselves
`require'd by yet-other-files, in which case they may end up being
loaded during compilation of those-yet-other-files, at which point
they'll also load this-file.  In that case, you want like to again
replace (require 'that-file) with (if t (require 'that-file)) to avoid
loading those-files during byte-compilation of those-yet-other-files.


        Stefan



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

* Re: eval-when-load?
  2015-07-16 23:03 ` eval-when-load? Stefan Monnier
@ 2015-07-17 10:07   ` Stephen Leake
  2015-08-06 22:50     ` eval-when-load? Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen Leake @ 2015-07-17 10:07 UTC (permalink / raw)
  To: emacs-devel

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

> But I suspect the problem you see is that the above form appears in
> a file which is not only byte-compiled but is also `require'd by other
> files in that package, so when *those* files are byte-compiled, the call
> to jde-jdhelper is indeed executed, since at that time this file is
> loaded (as part of byte-compiling those other files).

Yes, that's it.

> Maybe you want to use
>
>    (if t (require 'this-file))
>
> instead of (require 'this-file) so that this-file is only loaded when
> those-files are *loaded* rather than when they're byte-compiled.

This almost works; I still get errors, because jde-jdhelper is a macro,
and the variable itself is referenced, so I need this-file loaded at
compile-time. Sigh.

-- 
-- Stephe



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

* Re: eval-when-load?
  2015-07-17 10:07   ` eval-when-load? Stephen Leake
@ 2015-08-06 22:50     ` Stefan Monnier
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2015-08-06 22:50 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

> This almost works; I still get errors, because jde-jdhelper is a macro,
> and the variable itself is referenced, so I need this-file loaded at
> compile-time. Sigh.

I'm not surprised.  It's usually easier&better to arrange for the
initialization code that signals the error to only run when the code is
really activated, rather than when the files are loaded.


        Stefan



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

end of thread, other threads:[~2015-08-06 22:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-16 16:33 eval-when-load? Stephen Leake
2015-07-16 18:13 ` eval-when-load? Artur Malabarba
2015-07-16 22:04   ` eval-when-load? Stephen Leake
2015-07-16 22:13 ` eval-when-load? Philipp Stephani
2015-07-16 23:03 ` eval-when-load? Stefan Monnier
2015-07-17 10:07   ` eval-when-load? Stephen Leake
2015-08-06 22:50     ` eval-when-load? Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).