如何在一个块内临时定义一个条件?

How to temporarily define a condition, within a block?

Common Lisp (specifically SBCL in my case), it is common to define exceptions (known as "conditions") using the define-condition宏中。这在当前包中全局定义了条件。

有没有办法在特定范围内本地定义条件?或者,或者,使用其他一些惯用的方法来提前退出具有值的范围,而不会污染全局名称空间?

(with-conditions
  (block
    (...)
    (prematurely-exit-block-with some-data)
    (...))
  (when-condition-happens (some data) ...))

Is there a way to define a condition locally, within a specific scope?

没有

Or, alternatively, some other idiomatic way to prematurely exit a scope with a value, without polluting the global namespace?

Common Lisp 有几种用于这种控制流的构造,其中返回一个或多个值:RETURNRETURN-FROMTHROW、...

  • RETURN returns 来自名为 NIL
  • 区块
  • RETURN-FROM returns 来自命名的 区块
  • THROW 跳转到通过 CATCH
  • 建立的 catch 标签

returnreturn-from 正在使用词法块。 catch 为其标签建立一个动态范围。