Generic function
In computer programming, a generic function is a function defined for polymorphism.
In computer programming, a generic function is a function defined for polymorphism. (send object :message) With New Flavors, it was decided the message should be a real function and the usual function calling syntax should be used: (message object) message now is a generic function, an object and function in its own right. Individual implementations of the message are called methods. The same idea was implemented in CommonLoops. New Flavors and CommonLoops were the main influence for the Common Lisp Object System. (defgeneric collide (object-1 object-2)) Methods belonging to the generic function are defined outside of classes. Here we define a method for the generic function collide which is specialized for the classes asteroid (first parameter object-1) and spaceship (second parameter object-2). The parameters are used as normal variables inside the method body. There is no special namespace that has access to class slots. (defmethod collide ((object-1 asteroid) (object-2 spaceship)) (format t "asteroid ~a collides with spaceship ~a" object-1 object-2)) Calling the generic function: ? (collide (make-instance 'asteroid) (make-instance 'spaceship)) asteroid # collides with spaceship # Common Lisp can also retrieve individual methods from the generic function. FIND-METHOD finds the method from the generic function collide specialized for the classes asteroid and spaceship. ? (find-method #'collide nil (list (find-class 'asteroid) (find-class 'spaceship)))
References
No reference rows published for this article.
Cite this article
MLA
"Generic function." encyclopegia.org, 2025, https://encyclopegia.org/wiki/generic-function. Accessed 23 November 2026.
Chicago
"Generic function." encyclopegia.org. Accessed November 23, 2026. https://encyclopegia.org/wiki/generic-function.
APA
encyclopegia.org. (2025). Generic function. Retrieved November 23, 2026, from https://encyclopegia.org/wiki/generic-function
Accessed November 23, 2026 · CC BY-SA 4.0 · citation page · JSON