Generic function: Difference between revisions

Content deleted Content added
Monkbot (talk | contribs)
m In Common Lisp Object System: Task 16: replaced (1×) / removed (0×) deprecated |dead-url= and |deadurl= with |url-status=;
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 11:
One 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 to send a message 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''.
Line 28:
=== 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 ===
Line 55:
<!-- i moved this earlier because i think it's the more common definition. C++, Java, C#, ML , Rust,
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
<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. -->