0

I am experimenting with writing elisp in a more scheme-y style where helper functions are definded inside another function's lexical envirnonment like this (with lexical binding turned on):

(defun my-func (param)
  "Outer function."
       
  (defun helper ()
    "Inner function."
    (do-stuff param))

  (helper))

I do this to not pollute the global environment with a lot of small helper functions.

But now I want to test the helper function with ERT and cannot do it, because it is not in the global namespace.

Is it possible to turn off lexical binding for testing? If I evaluate the with eval-buffer with lexical binding disabled, the helper function still doesn't appear in the global environment. Might there be another way?

EDIT:

I just noticed that when my-func gets evaluated, it will put helper in the global environment. Thus, these nested functions are unnecessary.

1 Answer 1

0

defun always puts the function into the global environment. flet creates functions that are defined only within their lexical scope.

Incidentally, I wouldn’t try to write tests for functions defined with flet. They cannot be called by anything outside the scope of their definition, which will be some function that you can test. If the outer function works, then the functions inside of it be working as well.

2
  • Are the flet function definitions evaluated every time the function is called?
    – dandy
    Commented Sep 3, 2022 at 11:24
  • I’m pretty sure the compiler compiles them the first time they are evaluated.
    – db48x
    Commented Sep 3, 2022 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.