Generic Functions
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a generic function is a function defined for polymorphism.


In statically typed languages

In statically typed languages (such as
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
and
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
), the term ''generic functions'' refers to a mechanism for ''compile-time polymorphism'' (
static dispatch In computing, static dispatch is a form of polymorphism fully resolved during compile time. It is a form of ''method dispatch,'' which describes how a language or environment will select which implementation of a method or function to use. Ex ...
), specifically
parametric polymorphism In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
. These are functions defined with
TypeParameter In computer programming languages, TypeParameter is a generic label used in generic programming to reference an unknown data type, data structure, or class. TypeParameter is most frequently used in C++ templates and Java generics . TypeParameter i ...
s, intended to be resolved with
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
type information. The compiler uses these types to instantiate suitable versions, resolving any
function overloading In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
appropriately.


In Common Lisp Object System

In some systems for
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
such as the
Common Lisp Object System The Common Lisp Object System (CLOS) 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 ...
(CLOS) 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 ''The Art of the Metaobject Protocol'' (AMOP) is a 1991 book by Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow (all three working for Xerox PARC) on the subject of metaobject protocol. Overview The book contains an explanation of what a m ...
'' explains the implementation and use of CLOS generic functions in detail. One of the early object-oriented programming extensions to Lisp is
Flavors Flavor or flavour is either the sensory perception of taste or smell, or a flavoring in food that produces such perception. Flavor or flavour may also refer to: Science *Flavors (programming language), an early object-oriented extension to Lis ...
. It used the usual message sending paradigm influenced by
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
. 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 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 (the Common Lisp Object-Oriented Programming System; an acronym reminiscent of the earlier Lisp OO system "Loops" for the Interlisp-D system) is an early programming language which extended Common Lisp to include Object-oriented progra ...
. 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 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 is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
terms
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
, 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 computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented ...
). In a programming language with
multiple dispatch Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of ...
when a generic function is called, method dispatch occurs on the basis of all arguments, not just one which is privileged.
New Flavors Flavors, an early object-oriented extension to Lisp developed by Howard Cannon at the MIT Artificial Intelligence Laboratory for the Lisp machine and its programming language Lisp Machine Lisp, was the first programming language to include mixin ...
also provided generic functions, but only single dispatch.


References

{{DEFAULTSORT:Generic Function Method (computer programming)