Generic function: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile app edit
mNo edit summary
 
(15 intermediate revisions by 10 users not shown)
Line 1:
{{Distinguish|generalizedGeneralized function}}
{{Polymorphism}}
In certain systems for [[object-oriented programming]] such as the [[CLOS|Common Lisp Object System]]<ref>[http://www.dreamsongs.com/Files/ECOOP.pdf The Common Lisp Object System: An Overview]</ref> and [[Dylan programming language|Dylan]], 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 [[Inheritance (object-oriented programming)|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 usage of CLOS generic functions in detail.
 
In [[computer programming]], a '''generic function''' is a function defined for [[Polymorphism (computer science)|polymorphism]].
[[Flavors (programming language)|Flavors]] is one of the early object-oriented programming extensions to Lisp. It used the usual message sending paradigm influenced by [[Smalltalk]]. The syntax for sending a message in Flavors is:
 
== In statically typed languages ==
<source lang="lisp">
In statically typed languages (such as [[C++]] and [[Java (programming language)|Java]]), the term ''generic functions'' refers to a mechanism for ''compile-time polymorphism'' ([[static dispatch]]), specifically [[parametric polymorphism]]. These are functions defined with [[TypeParameter]]s, 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 certainsome systems for [[object-oriented programming]] such as the [[CLOS|Common Lisp Object System]] (CLOS)<ref>[http://www.dreamsongs.com/Files/ECOOP.pdf The Common Lisp Object System: An Overview]</ref> and [[Dylan (programming language)|Dylan]], 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 [[Inheritance (object-oriented programming)|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 usageuse of CLOS generic functions in detail.
 
[[Flavors (programming language)|Flavors]] is oneOne of the early object-oriented programming extensions to Lisp is [[Flavors (programming language)|Flavors]]. It used the usual message sending paradigm influenced by [[Smalltalk]]. The Flavors syntax forto sendingsend a message in Flavors is:
 
<sourcesyntaxhighlight lang="lisp">
(send object :message)
</syntaxhighlight>
</source>
 
With New Flavors, it was decided the message should be a real function and the usual function calling syntax should be used:
 
<sourcesyntaxhighlight lang="lisp">
(message object)
</syntaxhighlight>
</source>
 
''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]].<ref>[{{Cite web |url=http://www2.parc.com/istl/groups/gir/papers/stefik-commonloops-oopsla66.pdf |title=CommonLoops, Merging Lisp and Object-Oriented Programming] |access-date=2009-12-10 |archive-url=https://web.archive.org/web/20110604013117/http://www2.parc.com/istl/groups/gir/papers/stefik-commonloops-oopsla66.pdf |archive-date=2011-06-04 |url-status=dead }}</ref> 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''.
<sourcesyntaxhighlight lang="lisp">
(defgeneric collide (object-1 object-2))
</syntaxhighlight>
</source>
 
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.
<sourcesyntaxhighlight lang="lisp">
(defmethod collide ((object-1 asteroid) (object-2 spaceship))
(format t "asteroid ~a collides with spaceship ~a" object-1 object-2))
</syntaxhighlight>
</source>
 
Calling the generic function:
<sourcesyntaxhighlight lang="lisp">
? (collide (make-instance 'asteroid) (make-instance 'spaceship))
asteroid #<ASTEROID 4020003FD3> collides with spaceship #<SPACESHIP 40200048CB>
</syntaxhighlight>
</source>
 
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''.
<sourcesyntaxhighlight lang="lisp">
? (find-method #'collide nil (list (find-class 'asteroid) (find-class 'spaceship)))
#<STANDARD-METHOD COLLIDE NIL (ASTEROID SPACESHIP) 4150015E43>
</syntaxhighlight>
</source>
 
=== Comparison to other languages ===
Generic functions correspond roughly to what [[Smalltalk]] callsterms [[Method (computer science)|methods]], 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]] when a generic function is called, method dispatch occurs on the basis of all arguments, not just aone singlewhich privilegedis oneprivileged. [[Flavors (programming language)|New Flavors]] also provided generic functions, but only single dispatch.
 
In JavaScript, a generic function is a function that can work with values of different types, rather than a specific type. This is achieved through the use of type parameters or by dynamically checking the type of the value being operated on. One common use case for generic functions in JavaScript is to create reusable functions that can work with different data types, such as arrays, strings, or objects. JavaScript's dynamic typing system makes it particularly suited for the creation of generic functions, as values can be easily coerced or converted to different types as needed.
Generic functions correspond roughly to what [[Smalltalk]] calls [[Method (computer science)|methods]], 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]] when a generic function is called, method dispatch occurs on the basis of all arguments, not just a single privileged one. [[Flavors (programming language)|New Flavors]] also provided generic functions, but only single dispatch.
 
<!-- i moved this earlier because i think it's the more common definition. C++, Java, C#, ML , Rust,
Anotheranother, completely separate definition of '''generic function''' is a function that uses [[Type_polymorphism#Parametric_polymorphism|parametric polymorphism]]. This is the definition used when working with a language like [[OCaml]]. An example of a generic function is
<sourcesyntaxhighlight lang="ocaml">
let id : 'a -> 'a = fun x -> x
</syntaxhighlight>
</source>
which takes an argument of any type and returns something of that same type. -->
 
== References ==
{{Reflist}}
<references/>
 
{{DEFAULTSORT:Generic Function}}