Generic function
Encyclopedia
In certain systems for object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 such as the Common Lisp Object System
CLOS
The Common Lisp Object System is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a powerful dynamic object system which differs radically from the OOP facilities found in more static languages such as C++ or Java. CLOS was inspired by earlier Lisp object...

 and Dylan
Dylan programming language
Dylan is a multi-paradigm programming language that includes support for functional and object-oriented programming, and is dynamic and reflective while providing a programming model designed to support efficient machine code generation, including fine-grained control over dynamic and static...

, a generic function is an entity made up of all methods having the same name. Typically a generic function itself 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
The Art of the Metaobject Protocol
The Art of the Metaobject Protocol is a 1991 book by Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow on metaobject protocol...

 explains the implementation and usage of CLOS generic functions in detail.

Flavors is one of the early object-oriented extensions to Lisp. It used the usual message sending paradigm influenced by Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

. The syntax for sending a message in Flavors is:


(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
CommonLoops
CommonLoops is an early programming language which extended Common Lisp to include Object-oriented programming functionality and is a dynamic object system which differs from the OOP facilities found in static...

. New Flavors and CommonLoops were the main influence for the Common Lisp Object System.

Common Lisp

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

(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)))


Comparison to other languages

Generic functions correspond roughly to what Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

 calls methods
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

, with the notable exception that, in Smalltalk, the receiver's class is the sole determinant of which body of code is actually called: the types or values of the arguments are irrelevant (single dispatch). In a programming language with multiple dispatch
Multiple dispatch
Multiple dispatch or multimethods or function overloading is the feature of some object-oriented programming languages in which a function or method can be dynamically dispatched based on the run time type of more than one of its arguments...

 when a generic function is called, method dispatch occurs on the basis of all arguments, not just a single privileged one. New Flavors also provided generic functions, but only single dispatch.

Another, completely separate definition of generic function is a function that uses parametric polymorphism. This is the definition used when working with a language like OCaml. An example of a generic function is

id: a->a
let id a = a

which takes an argument of any type and returns something of that same type.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK