Apply a method

Discussion of Common Lisp
Post Reply
Posts: 8
Joined: Mon Jul 04, 2011 7:00 am

Apply a method

Post by underablackrainbow » Tue Nov 20, 2012 1:15 am

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.

Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Re: Apply a method

Post by Goheeca » Tue Nov 20, 2012 1:37 am

I think it's not intended for this purpose. Maybe through MOP.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: Apply a method

Post by pjstirling » Tue Nov 20, 2012 10:29 am

What is the goal here? You can apply generic functions, can't you?

Posts: 62
Joined: Fri Jun 10, 2011 6:38 am

Re: Apply a method

Post by Konfusius » Tue Nov 20, 2012 4:42 pm

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.
Last edited by Konfusius on Thu Nov 22, 2012 4:27 am, edited 1 time in total.

Posts: 98
Joined: Thu Jul 10, 2008 7:14 pm

Re: Apply a method

Post by JamesF » Thu Nov 22, 2012 2:02 am

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?

Post Reply