Permalink

Type Inference in Nice

29 MAR 2004

In the Nice CVS repository, Daniel's putting the finishing touches on a great new feature: polymorphic type inference.

For a long time now, it's been possible to omit type declarations for monomorphic types (that is, types that aren't parameterized - like String, or int, but not ArrayList). So you could do things like this:


let name = "Nice";

let version = 0.97;

and it would be just as if you had typed:

let String name = "Nice";

let float version = 0.97;

Now, though, Nice supports polymorphic type inference, which means that Nice can almost always figure out the right types for your variables, even the parameterized ones, without any help from the programmer:


let list = new ArrayList();

list.add("A");

list.add(2); //Compile error!

The compiler can tell from the fact we added a string to the list, that the list can only contain strings. When we later try to add a number, we get a compile-time error. All without writing a single type declaration. This is a feature normally found in functional languages like Haskell, not often (ever?) in object oriented languages for the JVM.