Advice (programming): Difference between revisions

Content deleted Content added
Quote indent
expand
Line 1:
In [[aspect-oriented programming|aspect]] aand [[functional programming]], '''piece of advice''' describes a class of [[function]]s which modify other functions when the latter are run; it is a certain function, method or procedure that is to be applied at a given [[join point]] of a program.
 
The following is taken from a discussion at the mailing list [http://aosd.net/pipermail/discuss_aosd.net/2004-November/001173.html aosd-discuss]. [[Pascal Costanza]] contributed the following:
Line 16:
 
Since method combination and macros are closely related, it's also interesting to note that the first macro system was described in 1963, three years before Warren Teitelman's PhD thesis. See AIM-57 at http://www.ai.mit.edu/research/publications/browse/0000browse.shtml <sup>[[#Notes|2]]</sup>
 
==Use==
The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot be easily modified or extended. The [[Emacspeak]] [[Emacs]]-addon makes extensive use of advice: it must modify thousands of existing Emacs modules and functions such that it can produce audio output for the blind corresponding to the visual presentation, but it would obviously be infeasible to copy all of them and redefine them to produce audio output in addition to their normal outputs; so, the Emacspeak programmers define advice functions which run before and after.
 
Another Emacs example; suppose after one corrected a misspelled word through [[ispell]], one wanted to re-spellcheck the entire buffer. M-$ offers no such functionality, even if the spellchecked word is used a thousand times. One ''could'' track down the definition of <code>ispell-word</code>, copy it into one's .emacs, and write the additional functionality, but this is tedious, prone to broken-ness (the .emacs version will get out of sync with the actual Ispell Elisp module, if it even works out of its home). What one wants is fairly simple: just to run another command after <code>ispell-word</code> runs. Using advice functions, it can be done as simply as this:
 
<code>
(defadvice ispell (after advice)
(flyspell-buffer))
(ad-activate 'ispell t) </code>
 
==Notes==
[[Gregor Kiczales]] comments the above as follows:
# ''Advice appeared separately from [[Flavors (Lisp)|Flavors]] in [[Maclisp]] and the [[Lisp Machine]]. You could advise any function, just like in Interlisp at the time. The before/after ontology appeared separately in Flavors methods.''
# ''Method combination and macros were only marginally related until much later, in New Flavors and [[CLOS]], when a macro-like mechanism was provided to allow people to define their own rules for combining methods. Prior to that the rules governing combination of before/after methods and so-called whoppers methods (around) was fixed, and the compiler just generated the code for that. There were things called wrappers, which had macro-like behavior, but I forget when they came around. Traipsing through the various versions of MacLisp and Lispm manual to get this part of the history exactly right could interesting. Or it could be that Howard Cannon or David Moon or someone could actually remember it all exactly.''
 
==External links==
* [http://www.ai.mit.edu/research/publications/browse/0200browse.shtml Teitelman's PhD thesis] (AITR-221)
* [http://www.classiccmp.org/bitsavers/pdf/xerox/interlisp/1974_InterlispRefMan.pdf InterLisp reference manual]
* [http://p-cos.blogspot.com/2007/12/origin-of-advice.html "Origin of Advice"]
 
[[Category:Aspect-oriented programming|Aspect-oriented programming, Advice]]