Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:09:23+00:00 2026-05-30T19:09:23+00:00

How can i specialize a generic function to take symbols designating subclasses of given

  • 0

How can i specialize a generic function to take symbols designating subclasses of given class.
For example:

(defclass a () ())
(defclass b (a) ())
(defclass c (b) ())
(defclass d () ())

(defgeneric fun (param))
(defmethod fun ((param (<subclass of> a)))
  (format t "~a is a subclass of A~%" param))

(fun 'c) ;-> "C is a subclass of A"
(fun 'd) ;-> Error: not found method for generic function call (fun 'd)

Is such dispatching possible with CLOS? And if it is, what should I write instead of “subclass of“?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:09:24+00:00Added an answer on May 30, 2026 at 7:09 pm

    You won’t be able to easily perform this exact task using only CLOS dispatching.

    Before I continue, I think some brief notes on terminology is important.

    The Common Lisp HyperSpec glossary defines “subclass” in this way:

    a class that inherits from another class, called a superclass. (No
    class is a subclass of itself.)

    This definition, while intuitive, seems odd to me as I’d expect that to be the definition of a “proper subclass”. However, all classes are types, and it defines “subtype” as:

    a type whose membership is the same as or a proper subset of the membership of another type, called a supertype. (Every type is a subtype of itself.)

    Note the parenthetical: “Every type is a subtype of itself.”

    It also defines a “proper subtype”:

    (of a type) a subtype of the type which is not the same type as the type (i.e., its elements are a “proper subset” of the type).

    So, in your example, B and C are subclasses of A, and also subtypes. On the other hand B, C, and A are subtypes of A.

    The thing one puts in defmethod is a “parameter specializer name”. It can be a symbol, a class (which is a little hard to type), or a list starting with eql. If you provide a symbol, it specifies the class named by that symbol (which is, of course, a type). An eql list specifies a type consisting of objects which are eql to the thing in the list.

    The method will match any object which is a member of the type the specializer specifies. And of course, a member of a subtype of X is also a member of X.

    So your first problem is that you are passing symbol objects to your method; every symbol is of type SYMBOL. A symbol that happens to name a class is no different in this respect; it’s only relationship to the class is that it is the class’s name, which is not a subtype relation.

    There are class objects (returned by find-class), but they’re no better than symbols for method specialization here because the type of a class object is usually the same as the type of its subclasses’ class objects.

    So, you’re left using instances or reading AMOP to learn how to create your own types of generic functions.

    Once you have an instance, you can write the method like this:

    (defmethod fun ((param a))
      (if (eq (type-of param) 'a)
        (call-next-method)
        (format t "~a is a subclass of A~%" (type-of param))))
    

    If you have an easy way to retrieve instances of your classes, you could write this wrapper:

    (defmethod fun ((param symbol))
      (fun (retrieve-instance param)))
    

    Then you’ll be able to pass symbols to fun and get the results you want.

    If you want to use AMOP functions (which were not specified by the standard but are widely available, see Closer Project), you can define retrieve-instance like this:

    (defun retrieve-instance (name)
      (let ((class (find-class name)))
        (unless (class-finalized-p class)
          (finalize-inheritance class))
        (class-prototype class)))
    

    Note that method dispatch is just about the only thing the result of class-prototype is good for; don’t try to modify it or anything like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I go about determining return type of a member generic function? template<class
I have a generic function for pushing stuff on to the lua stack called
I've got a generic class that manages resources of all kinds of types, but
One of my classes declares a templated function: template<class A, class B> A do_something(const
I'm currently refactoring some code the explicitly specializes a member function of a class
Can I serialize a generic list of serializable objects without having to specify their
I wonder if it is in any way possible to specialize generic interface methods
Given the following inheritance tree: I have a public class BaseForm : Form This
I want to inherit from some kind of array/vector/list class so that I can
I am looking for a tool that can serialize and/or transform SQL Result Sets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.