Duck typing |
In computer science, duck typing is a term for the dynamic typing typical of some programming languages, such as with Smalltalk programming language, where the variable value itself determines what it can do. It also implies that as long as an object implements a certain interface, it is interchangeable with any other object that implements the same interface (computer science), no matter whether the two have a related inheritance (object-oriented programming) hierarchy.
The name combines two metaphors, both to make a serious point, and to make a joke. Initially used in the context of dynamic typing by Dave Thomas in the Ruby programming language community, its premise is that if it walks like a duck, and talks like a duck, then it might as well be a duck . One can also say that the language ducks the issue of typing.
=Duck Typing in Python=
Duck typing is heavily used in Python, citing from the Python Tutorial [http://docs.python.org/tut/node18.html#l2h-46 (Appendix D)]:
:; duck-typing :: Pythonic programming style that determines an object s type by inspection of its method or attribute signature rather than by explicit relationship to some type object ( If it looks like a duck and quacks like a duck, it must be a duck. ) By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs hasattr() tests or EAFP [Easier to Ask Forgiveness than Permission] programming.
The standard example of duck typing in Python is file-like classes. Classes can implement some or all of the methods of file and be used where files would normally be used. For example, [http://docs.python.org/lib/module-gzip.html GzipFile] implements a file-like object for accessing Gzip-compressed data. [http://docs.python.org/lib/module-cStringIO.html cStringIO] allows treating a Python string as a file. Sockets and files share many of the same methods as well, however, sockets [http://diveintopython.org/http_web_services/gzip_compression.html#d0e29389 lack the tell() method] and can t be used everywhere a GzipFile can be used. This shows the flexibility of duck typing: a file-like object can implement only methods it is able to, and consequently it can be only used in situations where it makes sense.
=Duck Typing in Java=
In 2003, Dave Orme, leader of Eclipse s [http://www.eclipse.org/vep Visual Editor Project] was looking for a generic way to bind any Standard_Widget_Toolkit control to any JavaBeans-style object, sometimes known as a POJO, short for Plain Old Java Object.
He noticed that SWT reuses names religiously across its class hierarchy. For example, to set the caption of something, you normally set the Text property. This is true for an SWT Shell (window), a Text control, a Label, and many more SWT controls. David realized that if he could implement data binding in terms of the methods that are implemented on a control, that he would save considerable work and achieve a much higher level of code reuse compared to implementing separate data binding for an SWT Shell, Text, Label, and so on. When the Ruby community started describing this kind of type system as Duck Typing , David realized that he had simply rediscovered what Smalltalk, Ruby, Python and other programmers had known for a long time.
Later, David formalized this knowledge by creating a class that makes duck typing simple and natural for Java programmers. This class can be found on his blog under the entry [http://www.coconut-palm-software.com/the_visual_editor/2005/04/29#duck_type Java Does Duck Typing].
Later, Cedric Beust wrote [http://www.beust.com/weblog/archives/000269.html a blog entry] cautioning about the possible perils of duck typing.
=External links=
|
|