0

I have a situation where I have a function that depends on other functions. So

(defun a ()
   (something))

and

(defun b ()
   (something-else))

are used by

(defun c ()
   (something-calling (a) (b))

Not wanting to do a blanket load of everything -- and not really wanting to rewrite to include a and b in a cl-flet block -- would I change c to use eval, or is there any trick to have c autoload what it is dependent upon? I've seen autoload, but I'm not sure if it's applicable here.

I guess I'd like to be able to talk to the elisp "live namespace" and tell it, "If you see this function called, go ahead and load/initialize these dependent functions/variables."

Drew
  • 77,472
  • 10
  • 114
  • 243
147pm
  • 2,959
  • 1
  • 18
  • 42

1 Answers1

4

You don't need autoload here. Just put (require 'a-lib) and (require 'b-lib) inside function c itself (assuming a-lib and b-lib are the libraries that define functions a and b, respectively).

That is, let function c load the libraries its code needs. They will be loaded only the first time (and only if they have not already been loaded).

(defun c ()
  (require 'a-lib)
  (require 'b-lib)
  (something-calling (a) (b))
Drew
  • 77,472
  • 10
  • 114
  • 243
  • Yes, require would be good, but what if a and b are not in the same file? I want them to be anywhere. Could I do some sort of path or net location? Again, I don't want to always be doing require on a whole "package" and I don't want to have to break down things to one function per file, either. Library of Babel allowed that, only very primitively. – 147pm Nov 24 '15 at 02:03
  • The example I gave does not assume they are in the same file. It assumes what it says: that a is in file a-lib.el and b is in file b-lib.el. If you have no idea where they are then all you can do is punt. You either have the necessary libraries in your load-path or you tell Emacs to load them, one way or another. Even an autoload specifies which library defines a given function. – Drew Nov 24 '15 at 02:23
  • So yeah, if I don't do literally one function per file, I can't really call just one individual function. Again LOB is the only way for this in the Emacs elisp world. How about aliasing? Can I have a function actually somewhere else, but have a "stub" in the buffer I'm using (like CORBA)? Update: I just found corba.el. I'll take a look. . . . – 147pm Nov 24 '15 at 03:10
  • @147pm You're over-engineering, imho. –  Nov 27 '15 at 13:56
  • @lunaryorn Ha! Maybe so. But I just don't like the globbiness of code. I want it to be airy. For example, I write a Package-A, but I want to write a Package-B that uses some subset of Package-A. Now it only seems like I can manually "copy" into Package-B what I need, or I can require 'Package-A. That seems clumsy and globby. I'd like a registry of functions and data where I can just go shopping and plug things (e.g. functions) together like lego blocks. BTW, I don't think I invented this idea. – 147pm Nov 27 '15 at 21:54
  • You can also autload what you require, which was part of your original question, but that question is unclear in this regard. Autoloading loads a complete file. And that file might itself load others that it needs. There is no autoloading of only pieces of a file. Still, you can add (autoload...) sexps to your code to ensure that the files it requires are loaded. – Drew Nov 27 '15 at 22:27