Search code examples
error-handlingcommon-lispclos

Portable to use make-instance to make conditions?


In Common Lisp, is it portable to use make-instance instead of make-condition to make condition objects?

For example:

(define-condition foo (condition)
  ())

(make-condition 'foo)
(make-instance 'foo)

Does this have something to do with how conditions are placed in the CLOS class hierarchy? (subtypep 'condition 'standard-class) returns false in SBCL and CLISP. However, make-instance can make conditions in both implementations. Is this guaranteed by the standard?


Solution

  • No, it is explicitly not portable to do that. From the specification:

    Conforming code must observe the following restrictions related to conditions:

    • define-condition, not defclass, must be used to define new condition types.
    • make-condition, not make-instance, must be used to create condition objects explicitly.
    • The :report option of define-condition, not defmethod for print-object, must be used to define a condition reporter.
    • slot-value, slot-boundp, slot-makunbound, and with-slots must not be used on condition objects. Instead, the appropriate accessor functions (defined by define-condition) should be used