underablackrainbow wrote:Hi. Can someone tell me if a method can be used outside of a generic function?
- no, but you can pretend that it can, because invoking a generic function looks exactly the same as invoking a regular function (this is not a coincidence)
- yes, you should be able to do what you describe, though a little tweaking may be required
Methods only exist
in the context of a generic function, in the sense that the generic defines the API, and the method then implements that API in whatever manner is best suited to the class(es) on which it's specialised. If you just invoke defmethod without first defining a generic function, CL infers what the generic should look like and creates it automagically. So from that perspective, you can't use methods without generics being involved in some way, even if you don't explicitly make it happen.
Similarly, when you define a method without first defining its generic function, and then call that method with suitable arguments, you don't need to think in terms of the generic function; you can just look at it as applying that method to its arguments in the same way that you do with a function. However, what you're actually
doing is invoking the generic function, which then goes through the CLOS dispatch system, finds the most specific method that matches the arguments, and then calls on that.
The upshot is that you can think about it in whichever terms suit you (invoking a method vs invoking the generic function), though in the long term you're definitely better off getting comfortable with CLOS' dispatch system.
I'm glossing over a detail or two here, but hopefully that helps a bit.
Moving on to the code that you give as an example: what you seem to be doing there is improvising the basic skeleton of what CLOS does when it dispatches a generic function on an argument list. You're making extra work for yourself while still (if implicitly) involving all the system overhead that goes with using methods, while effectively using them as functions. Is there a particular reason you're not just using either CLOS or simple functions? What is it that you're trying to accomplish here?