A Funny Java Flavoured Look at the World

Monday, April 24, 2006

What is Duck Typing and What does Cedric Beust have to do with it

Whilst searching for a bit of blurb about Cedric Beust as he was one of the authors of a Java 5 Features article I typed his name into wikipedia and he had an entry on explaining what Duck Typing is. I have often heard of Duck typing and often wondered what exactly it was.

So I thought I would pass on the knowledge, wikipedia has this as a definition of Duck Typing

In computer science, duck typing is a term for dynamic typing typical of some programming languages, such as Smalltalk, where a variable's value itself determines what the variable can do. It also implies that an object is interchangeable with any other object that implements the same interface, regardless of whether the objects have a related inheritance hierarchy.

Dave Thomas, in the Ruby community, initially used the term in the context of dynamic typing.[citation needed] His premise could be explained by the folklore saying, "If it walks like a duck and quacks like a duck, it must be a duck." One can also say that the duck typing method ducks the issue of typing variables.

I have to be honest on the first pass of that explanation I still felt a bit confused. The way I understand it, it seems that it means that you don't need to add the type to the class, the compiler just works it out at runtime (e.g. "If it walks like a duck and quacks like a duck, it must be a duck"). I think I heard Cedric talking about this on a JavaPosse podcast. There are links on the wikipedia page for duck typing on Dynamic Typing. I'm afraid I am to lazy to read that page as it was rather long.

I think I have also heard that the newer languages like Ruby implement Duck typing, so hopefully in the future Java will Cherry pick that functionality and put it into Java.

So how does Duck typing fit into Java Currently, well Wikipedia has a link to this article

Java Does Duck Typing

This article has the description of Duck Typing as

Duck Typing is a powerful feature included primarily in newer languages. It provides the following capability: An object having all the methods described in an interface can be made to implement that interface dynamically at runtime, even if the object’s class does not include the interface in its implements clause.

So now my understanding of Duck Typing is, it is dynamically implementing an interface at runtime even if the class doesn't implement the interface at design (compile) time.

I'm not sure if I really understand what Duck Typing is or if I understand what it is but don't understand how/why you would want to use it.

if anyone would like to post a comment/link to what Duck Typing is and why it is useful then please do, I would greatly appreciate it.

There is one final thing to be cleared up here, What has Cedric Beust got to do with Ducks and their Typing. Well he has a blog entry linked to it on Wikipedia warning users about the perils of Duck Typing in Python

reading this article mentions the dangers of Duck Typing and that objects, it has this quote

the verification that the object does respond to such methods is not made when the object is passed as a parameter to the method, but on the invocation of the said methods, which explains why parameters to methods are usually not typed all

So Duck Typing sounds like it means the verification of an object is based on the methods, so you can pass any object as a parameter as long as it implemented a certain method. Which means many objects could be used in methods but without having to actually implement the interface on all of them. It sounds like Duck Typing takes away the need to implement interfaces on your objects but thenwhat'ss the harm in implementing an interface, apart from the effort of typing the line which says implements, especially if the class has the method.

well if I ever understand Duck Typing I will be sure to blog down the answer, I hope reading this has been a waste of your time because some one will hopefully write what Duck Typing is as a comment ;-)

9 Comments:

  • If normal Java had normal duck typing, you could do things like this:

    public whatever(Object list) {
    Object number = list.get(0);
    System.out.println(number.intValue() + 1);
    }

    Or whatever. Everything's an Object. No interfaces needed. Just call whatever method you want. It all becomes a hashtable lookup internally to see if that method is defined.

    So you see what type something is by how it's used. You don't explicitly state the type of each variable.

    By Anonymous Anonymous, at Mon Apr 24, 03:54:00 pm 2006  

  • If normal Java had normal duck typing, you could do things like this:

    public whatever(Object list) {
    Object number = list.get(0);
    System.out.println(number.intValue() + 1);
    }

    Or whatever. Everything's an Object. No interfaces needed. Just call whatever method you want. It all becomes a hashtable lookup internally to see if that method is defined.

    So you see what type something is by how it's used. You don't explicitly state the type of each variable.

    By Anonymous Anonymous, at Mon Apr 24, 03:55:00 pm 2006  

  • The simple explanation is that at compile-time, the language doesn't really care about types. At runtime, the system tries to use the object that was passed and if anything fails, it lets you know.

    This has some significant advantages in some areas, and disadvantages in others. In some dynamic languages you can make objects that decide at runtime which messages they want to respond to. This makes things like proxies incredibly easy.

    On the other hand, it's basically impossible for IDE and compilers to do good job of detecting a lot of errors in advance. A typical Ruby "IDE" won't tell you that the method you're trying to call doesn't exist -- because it can't really know.

    By Anonymous Anonymous, at Mon Apr 24, 07:18:00 pm 2006  

  • Thanks for the comments, I am clearer about what Duck Typing is and how it is used.

    It sounds like it could be quite difficult to read someone elses code and it could be easy to put in code that will error at runtime.

    I have heard that there aren't any good compilers for Ruby and other new languages and I suppose one of the reasons is because of this dynamic typing. It is also probably because Ruby is quite a new language and it takes quite a while and a lot of support for a good IDE to be developed.

    Once again thanks for the comments on a very interesting subject

    By Blogger The Hosk, at Mon Apr 24, 08:30:00 pm 2006  

  • Duck typing is more than just dynamic typing. Dynamic typing is a language feature, meaning that the compiler need not be able to compute that a certain method can always be invoked on a certain object. The evaluation of the message invocation is handled entirely at run time.

    Dyck typing isn't really a language feature as it is a design technique. It 's in some situations related to design by contract, actually. Instead of checking the type of an object (e.g. value instanceof Number) you check whether it "supports" the message you want to send it (e.g. value.isNumeric()). The advantage of using the second style over the first is that other kinds of objects that will answer in the affirmative to isNumeric (such as a Complex object) will be valid arguments for the piece of code in question.

    By Anonymous Anonymous, at Wed Apr 26, 09:15:00 am 2006  

  • Also, another minor remark pertaining to Ruby: it's actually older than Java ('91 I think). It just hasn't been in the mainstream for long.

    By Anonymous Anonymous, at Wed Apr 26, 09:16:00 am 2006  

  • I would want to remark that, as wouter suggested, "Duck Typing" would be not only related to dynamic typing, but it's also a sound static typing feature if supported by the language.

    For instante, this regular signature
    public whatever(Comparable object){

    forces the user to make their objects explicitly implement the interface "Comparable" if used as arguments for the method. The problem arises when you are not the coder of the object but know for sure it works because it has "compareTo" method in it. So, the compiler only has take care that your object "implement" the interface by looking at the methods.

    This is related to "contract oriented programming", that is not a very evolutioned concept, since it's only well adopted in C++ (with templates) and Eiffel (prerequisites, postrequisites, invariants). In fact, many more COP features can be implemented, such as asserting that the arguments are non-null, or non-modifiable, etc.

    By Blogger Daviti, at Wed Apr 26, 12:11:00 pm 2006  

  • thanks for the comments, I am the first to admit my knowledge of Duck typing is very poor, which was the main reason I thought I would try to find out what it was.

    I think I was struggling to understand the idea of Duck Typing because it seems to be very un Java. It seems in someways to be the complete opposite of the recent Generic code feature added to Java (e.g. type safe collection etc).

    I have found the comments just as useful, if not more than the actual research I did finding out the initial definition of Duck Typing. Duck Typing sounds like almost an impossible mix of Design by Contract and yet this isn't done until run time

    once again thanks for people who have posted comments and helped explain it more clearly to me

    By Blogger The Hosk, at Thu Apr 27, 12:44:00 am 2006  

  • Back in Visual Basic 4 or 5 I used to call this "polymorphism by coincidence". Given an object you can try a method on it. If it has the method it works, otherwise you get an error. Well, "works" is up for definition or debate. It happens to have a name you were looking for; no guarantees at all what it might do.

    By Anonymous Anonymous, at Thu Apr 27, 02:32:00 am 2006  

Post a Comment

<< Home