Page 1 of 1

Apply a method

PostPosted: Tue Nov 20, 2012 1:15 am
by underablackrainbow
Hi. Can someone tell me if a method can be used outside of a generic function? For example something like this:

Code: Select all
(defmethod xyz ((a integer)) a)
(setq x (find-method ...))
(apply x 4)


Thank you very much.
Tommy.

Re: Apply a method

PostPosted: Tue Nov 20, 2012 1:37 am
by Goheeca
I think it's not intended for this purpose. Maybe through MOP.

Re: Apply a method

PostPosted: Tue Nov 20, 2012 10:29 am
by pjstirling
What is the goal here? You can apply generic functions, can't you?

Re: Apply a method

PostPosted: Tue Nov 20, 2012 4:42 pm
by Konfusius
underablackrainbow wrote:Hi. Can someone tell me if a method can be used outside of a generic function?

Yes. They can be used just like ordinary functions.

EDIT: this is nonsense. i mixed up methods with generic functions.

Re: Apply a method

PostPosted: Thu Nov 22, 2012 2:02 am
by JamesF
underablackrainbow wrote:Hi. Can someone tell me if a method can be used outside of a generic function?


Short answers:
- 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?