In the old days, Emacs had no support for lexical scope. I am wondering how people dealt with a particular pitfall of dynamic scope in those days.
Suppose Alice writes a command my-insert-stuff
which relies on the fp-repeat
function defined in fp.el
(which we suppose is a library providing lots of functions for functional programming written by Bob) and suppose fp-repeat
is for repeatedly calling a function many times.
Part of contents of init.el
from Alice:
(require 'fp)
(defun my-insert-stuff ()
(interactive)
;; inserts "1111111111\n2222222222\n3333333333" to current buffer
(dolist (i (list "1" "2" "3"))
(fp-repeat 10
(lambda ()
(insert i)))
(insert "\n")))
Part of contents of fp.el
from Bob:
(defun fp-repeat (n func)
"Calls FUNC repeatedly, N times."
(dotimes (i n)
(funcall func)))
Alice soon finds that her command doesn't work like she expects. That's because Alice's use of i
and Bob's use of i
collide. In the old days, what could Alice or/and Bob do to prevent this kind of collision from happening?
Maybe Bob could change the docstring to
"Calls FUNC repeatedly, N times.
Warning: Never use i, n, func in FUNC body as nonlocal variables."