DenkzeitWiki

Suchen:

Aktuelle Änderungen Printable View Änderungen Bearbeiten

LexicalClosures > Closures > ClojureOnDotNet > MetaProgramming > PSPad2Wordpress > GoogleChartsAPI > CurriculumVitae > MarketingPython > AndersHejlsberg > FunctionObjects > Functor > TrampolineStyle > MaintainableCode > AbstractDataType > OopVsAdt > DesignByContract > GenericFunctionsClear Trail
Main /

Generic Functions

MultipleDispatch

OOBashing

It's a not-so-great name for something that might be better called a "polymorphic function", an "extensible function", or maybe even a "composable function", as these are all better descriptions of what a generic function is.[1]



In certain systems for object-oriented programming such as the Common Lisp Object System and Dylan, a generic function is an entity made up of all methods having the same name.

Generic functions correspond roughly to what Smalltalk calls messages; but when a generic function is called, method dispatch occurs on the basis of all arguments, not just a single privileged one. See under multiple dispatch for more. This is also known as a multimethod.[2]




Alternatively, I could use Dependency Injection and pass to the constructor all of its loggers, but this is also undesiderable, since I would be complicating the signature always, even if I needed to use a non-standard logger only very few times. I could use different factories to instantiate the class with different loggers, of course, but I think that the generic function approach is a much better solution. It clearly decouples the logging capabilities from the other features of the framework class, which stays simple. Client code does not need to touch the framework class, they just register their preferred logging function and it is done. Moreover generic functions are not just for logging: they are a general mechanism that you can use in many other circumstances.




Predicate Dispatch

Predicate-based dispatching goes considerably further, allowing real duck typing, e.g., you could implement a pformat method for everything that has an "__iter__" method and no "next" method (i.e., all iterables, but not iterators which could lead to unintentionally consuming the iterator).[4]





Adaptation


in Python


Edit - BackLinks - Tags - Page Hist - Print - Changes - Home - Orphans - Help

Zuletzt geändert am 29.09.2008 20:46 Uhr und seit 7. April 2005 1295 aufgerufen.