Object Commando languages, development and design

4Mar/092

Type Inference vs. Duck Typing

I've been working with JavaFX 1.0 lately, which has a new language called JavaFX Script. The language is based on Java, but is intended to make it easier and more concise to write Swing GUIs. The language has some interesting features and the first one I noticed was Type Inference. Having done some work in OCaml, I was pretty excited to see this. One point of confusion that I think a lot of people have is the difference between duck typing and type inference. It is not necessary to explicitly define the type in either of these type systems, so they look very similar on the surface. Dig a little deeper, and they are actually quite different.

Duck Typing

Duck Typing is a dynamic language concept that boils down to this: if an object responds to a message, that's all we need to know. Put another way, we may know that the object we're dealing with is an Employee, and we know Employee has a calculatePay() method, but at runtime, the fact that the object is an Employee doesn't matter, all that matters is that the object happens to have a calculatePay() method. Later, when we add a Consultant class, that has a calculatePay() method, we can treat it as if it were an Employee (in this particular case). The key point here is that it is dynamic. Only at run time does this stuff happen.

Type Inference

Type inference is when the compiler determines what type an object is based on the operations that are performed on it. The key part to this is the compiler makes this check, it is not at runtime. As an example, if we had the simple code:

private String example = "text";

Is the String declaration above really necessary? It's quite obvious that example is a String, since it is assigned the value right there. Now if we tried to treat the example as an Integer, it would fail. It would fail not because it doesn't have the right method, but because it's not the right type. Another example is below:

public int add(int x, int y)
  return x + y;
}

Here we have defined x and y as integers. With type inference, we would specify x and y and the compiler would know that since + was used on it, it must be a Number. With JavaFX, trying to pass a String into a type infered method that expects a string will result in a compile time error (and if using Netbeans, a nice set of red marks).

Comments (2) Trackbacks (0)
  1. Thanks for clarification. However both duck typing and type inference are used to infer the type of an object from the actions performed with it. In duck typing you explictly ask for specific methods and get an unnamed type that is defined by exactely this method only. In type inference you just use specific methods and the compiler infers the best matching predefined type. Could you please add a comment in this Wikipedia discussion because Wikipedia does not explain it very well.

  2. Hi Jakob,

    I’ll try explaining the difference on that wikipedia page. I didn’t even know that section was there.

    Thanks for the comment!

    -Ryan


Leave a comment

(required)

No trackbacks yet.