Sie sind auf Seite 1von 2

Generic function

In computer programming, a generic function is a function defined forpolymorphism.

Contents
In statically typed languages
In Common Lisp Object System
Example
Common Lisp
Comparison to other languages
References

In statically typed languages


In statically typed languages (such as C++ and Java), the term generic functions refers to a mechanism for compile-time
polymorphism (static dispatch), specifically parametric polymorphism. These are functions defined with TypeParameters, intended to
be resolved with compile time type information. The compiler uses these types to instantiate suitable versions, resolving any function
overloading appropriately.

In Common Lisp Object System


In some systems for object-oriented programmingsuch as the Common Lisp Object System (CLOS)[1] and Dylan, a generic function
is an entity made up of all methods having the same name. Typically a generic function is an instance of a class that inherits both
from function and standard-object. Thus generic functions are both functions (that can be called with and applied to arguments) and
ordinary objects. The book The Art of the Metaobject Protocol explains the implementation and use of CLOS generic functions in
detail.

One of the early object-oriented programming extensions to Lisp is Flavors. It used the usual message sending paradigm influenced
by Smalltalk. The Flavors syntax to send a message is:

(send object :message )

With New Flavors, it was decided the message should be a real function and the usual function call
ing 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.[2] New Flavors and CommonLoops were the main influence for the Common
Lisp Object System.

Example
Common Lisp
Define a generic function with two parameters object-1 and object-2. The name of the generic function collide.
is

(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 #<ASTEROID 4020003FD3> collides with spaceship #<SPACESHIP 40200048CB>

Common Lisp can also retrieve individual methods from the generic function. FIND-METHOD finds the method from the generic
function collide specialized for the classesasteroid and spaceship.

? (find-method #'collide nil (list (find-class 'asteroid ) (find-class 'spaceship )))


#<STANDARD-METHOD COLLIDE NIL (ASTEROID SPACESHIP ) 4150015E43>

Comparison to other languages


Generic functions correspond roughly to what Smalltalk terms methods, with the notable exception that, in Smalltalk, the receiver's
class is the sole determinant of which body of code is called: the types or values of the arguments are irrelevant (single dispatch). In a
programming language with multiple dispatch when a generic function is called, method dispatch occurs on the basis of all
arguments, not just one which is privileged.New Flavors also provided generic functions, but only single dispatch.

References
1. The Common Lisp Object System: An Overview(http://www.dreamsongs.com/Files/ECOOP.pdf)
2. CommonLoops, Merging Lisp and Object-Oriented Programming(http://www2.parc.com/istl/groups/gir/papers/stefik-
commonloops-oopsla66.pdf)

Retrieved from "https://en.wikipedia.org/w/index.php?title=Generic_function&oldid=807834007


"

This page was last edited on 30 October 2017, at 11:14.

Text is available under theCreative Commons Attribution-ShareAlike License ; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of theWikimedia
Foundation, Inc., a non-profit organization.

Das könnte Ihnen auch gefallen