Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Common Lisp === Define a generic function with two parameters object-1 and object-2. The name of the generic function is ''collide''. <syntaxhighlight lang="lisp"> (defgeneric collide (object-1 object-2)) </syntaxhighlight> 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. <syntaxhighlight lang="lisp"> (defmethod collide ((object-1 asteroid) (object-2 spaceship)) (format t "asteroid ~a collides with spaceship ~a" object-1 object-2)) </syntaxhighlight> Calling the generic function: <syntaxhighlight lang="lisp"> ? (collide (make-instance 'asteroid) (make-instance 'spaceship)) asteroid #<ASTEROID 4020003FD3> collides with spaceship #<SPACESHIP 40200048CB> </syntaxhighlight> 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''. <syntaxhighlight lang="lisp"> ? (find-method #'collide nil (list (find-class 'asteroid) (find-class 'spaceship))) #<STANDARD-METHOD COLLIDE NIL (ASTEROID SPACESHIP) 4150015E43> </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)