This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Specializing Methods on Keywords

4 posts · 1927 views

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

Re: Specializing Methods on Keywords

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.

Re: Specializing Methods on Keywords

Oh well. Thx!

Re: Specializing Methods on Keywords

You can do EQL specialization on a parameter. For instance:
(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/