--- title: Common Lisp argument precedence order parameterization of a generic function date: 2021-04-24 layout: post lang: en ref: common-lisp-argument-precedence-order-parameterization-of-a-generic-function --- When CLOS dispatches a method, it picks the most specific method definition to the argument list: ```lisp * (defgeneric a-fn (x)) # * (defmethod a-fn (x) :default-method) # * (defmethod a-fn ((x number)) :a-number) # * (defmethod a-fn ((x (eql 1))) :number-1) # * (a-fn nil) :DEFAULT-METHOD * (a-fn "1") :DEFAULT-METHOD * (a-fn 0) :A-NUMBER * (a-fn 1) :NUMBER-1 ``` CLOS uses a similar logic when choosing the method from parent classes, when multiple ones are available: ```lisp * (defclass class-a () ()) # * (defclass class-b () ()) # * (defgeneric another-fn (obj)) # * (defmethod another-fn ((obj class-a)) :class-a) ; Compiling LAMBDA (.PV-CELL. .NEXT-METHOD-CALL. OBJ): ; Compiling Top-Level Form: # * (defmethod another-fn ((obj class-b)) :class-b) ; Compiling LAMBDA (.PV-CELL. .NEXT-METHOD-CALL. OBJ): ; Compiling Top-Level Form: # ``` Given the above definitions, when inheriting from `class-a` and `class-b`, the order of inheritance matters: ```lisp * (defclass class-a-coming-first (class-a class-b) ()) # * (defclass class-b-coming-first (class-b class-a) ()) # * (another-fn (make-instance 'class-a-coming-first)) :CLASS-A * (another-fn (make-instance 'class-b-coming-first)) :CLASS-B ``` Combining the order of inheritance with generic functions with multiple arguments, CLOS has to make a choice of how to pick a method given two competing definitions, and its default strategy is prioritizing from left to right: ```lisp * (defgeneric yet-another-fn (obj1 obj2)) # * (defmethod yet-another-fn ((obj1 class-a) obj2) :first-arg-specialized) # * (defmethod yet-another-fn (obj1 (obj2 class-b)) :second-arg-specialized) # * (yet-another-fn (make-instance 'class-a) (make-instance 'class-b)) :FIRST-ARG-SPECIALIZED ``` CLOS has to make a choice between the first and the second definition of `yet-another-fn`, but its choice is just a heuristic. What if we want to the choice to be based on the second argument first? For that, we use the `:argument-precedence-order` option when declaring a generic function: ```lisp * (defgeneric yet-another-fn (obj1 obj2) (:argument-precedence-order obj2 obj1)) # * (yet-another-fn (make-instance 'class-a) (make-instance 'class-b)) :SECOND-ARG-SPECIALIZED ``` I liked that the `:argument-precedence-order` option exists. We shouldn't have to change the arguments from `(obj1 obj2)` to `(obj2 obj1)` just to make CLOS pick the method that we want. We can configure its default behaviour if desired, and keep the order of arguments however it best fits the generic function. ## Comparison with Clojure Clojure has an equivalent, when using `defmulti`. Since when declaring a multi-method with `defmulti` we must define the dispatch function, Clojure uses it to pick the method definition. Since the dispatch function is required, there is no need for a default behaviour, such as left-to-right. ## Conclusion Making the argument precedence order configurable for generic functions but not for class definitions makes a lot of sense. When declaring a class, we can choose the precedence order, and that is about it. But when defining a generic function, the order of argumentws is more important to the function semantics, and the argument precedence being left-to-right is just the default behaviour. One shouldn't change the order of arguments of a generic function for the sake of tailoring it to the CLOS priority ranking algorithm, but doing it for a class definition is just fine. TIL.