Seeing a positive correspondence between people who disdain generic 
functions and people who do not know what exactly generic functions are, 
I decided to write a little primer in hopes FUD propagation will make 
way for an augmented discussion.

---

Somewhat simplified, generic function is a set of functions bound to a 
single symbol. Where things get interesting is how a particular 
function* (let's call it specialisation) gets selected (dispatched). The 
process is usually deterministic, following a set of rules. Three 
predominant dispatch strategies are:

     1) type dispatch (also called multiple dispatch and overloading)

         A function is selected depending on types of arguments passed.

         Examples: C, Py3k?, PyProtocols.dispatch.on

     2) identity dispatch

         Usually used in conjunction with type dispatch

         A function is selected depending on concrete instances of 
arguments passed.

         Examples: CLOS

     3) predicate dispatch

         A function is selected depending on arbitrary (logical) 
expressions.

         Examples: Cecil, Mathematica, PyProtocols.distpatch.generic

The aforementioned rules usually also establishing a selection hierarchy 
("more specific" is often the governing relation) to reduce ambiguity.

While having more than one applicable specialisation may seem like a bad 
thing, it can actually be very useful. Enter function combination. 
Strictly speaking function combination is not a part of generic 
functions, therefore let's limit the scope to what PyProtocols (and 
CLOS) offer us.

The simplest combination strategy is allowing the selected 
specialisation to call the next most specific specialisation. What makes 
function combination transcend generic functions are secondary 
functions. These allow us to define (generic) functions that get called 
automatically in relation to another function (called primary). Hence we 
can have secondary functions be called before, after or around (same as 
composition) a primary function.


Some common "misconceptions":

1) Generic functions are an alternative to "normal" (Java) OOP

False. Message passing is nothing more than type dispatch on first 
(implicit) argument.

2) There is nothing I can't do without generic functions

True, however considering lambda calculus (or Turing completeness) this 
can be said for most language constructs.


References:

http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html
http://peak.telecommunity.com/DevCenter/CombiningResults
http://en.wikipedia.org/wiki/Multiple_dispatch
http://en.wikipedia.org/wiki/Generic_functions


* term method is often used

Cheers,
Simon


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to