In CLOS method definitions, can a specializer be a list of classes and not a single class?
-
29-05-2021 - |
题
While it does not make much sense in the general case as it should be done via superclasses, I want to allow "nil" for a particular parameter and avoid having to define a separate method.
I'm trying to do something like that (pseudo-code)
(defmethod my-method ((obj1 my-class1) (obj2 (or my-class2 null)))
method-body )
解决方案
This is not supported in plain CLOS.
Supported are:
- no class -> T
- EQL
- one class
其他提示
You could create a mixin class with no slots and add that to the superclass list of all the classes of interest; this makes sense because what you're doing implies that all the classes in your list conform to some type.
Alternatively don't specialize - write a method that is applicable to a superset of the types of interest but only acts on your subset. This may be easier to do but is less clean.
不隶属于 StackOverflow