Order of defmethod definitions in common lisp

  Kiến thức lập trình

Does the order in which I define defmethods play a role? Is it guaranteed that the first definition is tested first to see if the parameters match?

My mind-model of defmethod is, that it behaves like a cond where the first case is tested first.

(defun a (X)
    (cond ((typep X 'number) ...)
          ((typep X 'string) ...)
          ((typep X 'list) ...)))

If I know that the first case occurs most frequently in my application, then I include this case as the first in cond.

Is it the same with defmethod.

(defmetod a ((X number)) ...)

(defmetod a ((X string)) ...)

(defmetod a ((X list)) ...)

Is my mind model correct?

LEAVE A COMMENT