1
submitted 10 months ago by vfclists@alien.top to c/emacs@communick.news

By forward function declarations I mean what is needed when a function defined later in the file is called from a preceding one.

By circular I mean file A requires a function in file B which requires a function in file A. Something along those lines

PS. Apologies for inquiring about this again. I am sure I enquired sometime in the past and forgotten the actual question and the response to it.

top 2 comments
sorted by: hot top controversial new old
[-] db48x@alien.top 1 points 10 months ago

Mostly they just aren’t a problem. If you write a function A that calls some function B that doesn’t exist yet, you might get a warning. It’ll say something like “B is not known to be defined”. But the key thing to remember is that it doesn’t really care about B yet, not until you actually call A. Only then will it resolve B, checking to see if it is a real function or not, and then call it or error out.

But since most people get annoyed by warnings, they would ensure that A and B are in the same file. JDRiverRun also suggests using decare-function. If you read the docstring for that (using C-h f), you‘d get a link to chapter 13.16 Telling the Compiler that a Function is Defined of the Emacs Lisp Manual which describes some of the scenarios where it is useful.

[-] florommel@alien.top 1 points 10 months ago

As elisp is a dynamic language, it looks up these functions when needed. You can try it: E.g., evaluate this in the scratch buffer (C-x C-e):

(defun testfn1 ()
  (testfn2 "Test123"))

This works even though testfn2 is not defined. However, evaluating (testfn1) (actually calling the function) will give you an error as testfn2 is not found.

When you define testfn2, it is added to the global scope and thus becomes available for testfn1.

(defun testfn2 (text)
  (message "Msg: %s" text))

Now, (testfn1) works.

You can even define a new version of testfn2 which does something different. This way you can change any function in a lisp environment (this generally applies for scripting languages). This way also circular references are no problem. There may be warnings, when byte compiling functions with undefined references, but these are just warnings and compiling still works. If you look at a byte-compiled elisp file, you still see symbolic references to the called functions.

this post was submitted on 15 Nov 2023
1 points (100.0% liked)

Emacs

305 readers
1 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS