Specializing Methods on Keywords

Discussion of Common Lisp

Specializing Methods on Keywords

Postby Harnon » Sun Jun 21, 2009 8:40 am

I don't necessarily need it, but it would simplify things if i could specialize a method parameter on a keywords in general (not just one type), distinguishing them from
regular symbols. I've looked on google, but couldn't find anything.
When i try
(defmethod test ((a keyword))) or (defmethod test ((a key))) it gives the error keyword and key is not a class.
Thx! :D
Harnon
 
Posts: 78
Joined: Wed Jul 30, 2008 9:59 am

Re: Specializing Methods on Keywords

Postby Ramarren » Sun Jun 21, 2009 8:54 am

While KEYWORD is a type, it is not a class, and so you cannot specialize methods on them. All types which have corresponding systems classes are listed in the Hyperspec, and unfortunately keywords are not one of them. It seems to be just a flaw in the standard, since keywords form a subset of symbols, so there would be no ordering issues.

It seems the only thing to do is do a manual dispatch in the SYMBOL-specialized method using ETYPECASE or similar.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Specializing Methods on Keywords

Postby Harnon » Sun Jun 21, 2009 9:08 am

Oh well. Thx!
Harnon
 
Posts: 78
Joined: Wed Jul 30, 2008 9:59 am

Re: Specializing Methods on Keywords

Postby findinglisp » Tue Jun 23, 2009 9:37 am

You can do EQL specialization on a parameter. For instance:

Code: Select all
(defmethod foo (p1 (p2 (eql :bar))
    ...)

(defmethod foo (p1 (p2 (eql :quux))
    ...)
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests