Emacs Lisp如何在任意位置返回?

问题

我们知道Emacs Lisp并不像其他语言那样有return语句。那么,如果我想在程序的任意位置返回,那该如何做呢?

使用catch机制

有一种方法是,采用catch机制,下面是一个例子:

(defun lebe-fun-exit-any-arbitrary-point ()
  "Example show how to exit from a function at any arbitrary point in elisp."
  (interactive)
  (let ((a "a"))
    (catch 'return
      (when (equal "a" a)
        (throw 'return "non-local exit value"))
      (message "normal exit value"))))

执行下看:

ELISP> (lebe-fun-exit-any-arbitrary-point)
"non-local exit value"

参考: http://stackoverflow.com/questions/16248620/how-to-exit-from-a-function-at-any-arbitrary-point-in-elisp

你可能感兴趣的:(Emacs Lisp如何在任意位置返回?)