How to overcome the lack of local variable for emacs lisp closure

前端 未结 4 1692
忘了有多久
忘了有多久 2021-02-08 02:53

I\'m now studying Emacs Lisp from the reference manual and Common Lisp from a LISP Book.

from the Common Lisp book

>> (setf power-of-two
     (let          


        
4条回答
  •  孤独总比滥情好
    2021-02-08 03:19

    Update:

    By now, Emacs 24 has been officially released, and it supports lexical binding without using lexical-let, when the buffer-local variable lexical-binding is non-nil. See also M-: (info "(elisp) using lexical binding") and pokita's answer.


    You can use lexical-let from the Common Lisp Extensions (the "CL package"):

    elisp> (require 'cl)
    cl
    elisp> (setf power-of-two
                 (lexical-let ((previous-power-of-two 1))
                   #'(lambda ()
                       (setf previous-power-of-two
                             (* previous-power-of-two 2)))))
    (lambda
      (&rest --cl-rest--)
      (apply
       (lambda
         (G175638)
         (set G175638
              (*
               (symbol-value G175638)
               2)))
       '--previous-power-of-two-- --cl-rest--))
    
    elisp> (funcall power-of-two)
    2
    elisp> (funcall power-of-two)
    4
    elisp> (funcall power-of-two)
    8
    

    I've also heard about a lexbind branch of GNU Emacs.

提交回复
热议问题