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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容