Generic function Information & Generic function Links at HealthHaven.com
advertise
add site
services
publishers
database
health videos
Bookmark and Share

search wiki for    ?
web dir firms image gallery news pdf wiki shop video 
about
toolbar
stats
live show
health store
more stuff
JOIN/LOGIN
Featured Results:
 Generic , Generic , Generic , Generic Candy Cane, Generic , Generic ,...
Generic , Generic , Generic , Generic Candy Cane, Generic , Generic ,...
medused.com
 Biobloc,Generic Drugs,Generic Medicines,Generic...
Biobloc,Generic Drugs,Generic Medicines,Generic...
livemedinfo.com
 Butalbital, Generic Fioricet, cheapest butalbital and generic fioricet,...
Butalbital, Generic Fioricet, cheapest butalbital and generic fioricet,...
genericfioricet.net
  Generic Medicine, Generic Cialis, Levitra, Prozac, Zenegra, Softtabs,
Generic Medicine, Generic Cialis, Levitra, Prozac, Zenegra, Softtabs,
thetrainingstationinc.com
 

In certain systems for object-oriented programming such as the Common Lisp Object System[1] and Dylan, a generic function is an entity made up of all methods having the same name. Typically generic function is 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 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. The syntax for sending a message in Flavors is:

  (send object :message) 

With New Flavors it was decided the 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[2]. New Flavors and CommonLoops were the main influence for the Common Lisp Object System.

Contents

[edit] Example

[edit] 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 #<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 classes asteroid and spaceship.

 ? (find-method #'collide nil (list (find-class 'asteroid) (find-class 'spaceship))) #<STANDARD-METHOD COLLIDE NIL (ASTEROID SPACESHIP) 4150015E43> 

[edit] Comparison to other languages

Generic functions correspond roughly to what Smalltalk calls methods. 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. 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.

[edit] References

  1. ^ The Common Lisp Object System: An Overview
  2. ^ CommonLoops, Merging Lisp and Object-Oriented Programming



Product Results (view all...)

search wiki for    ?
web dir firms image gallery news pdf wiki shop video 



↑ top of page ↑about thumbshots