Matt's Mind

Tuesday, September 28, 2004

Nice!

This language called Nice is one of the few new languages I've seen recently that really looks like next step on from Java. It adds a lot of expressive, functional capabilities that I've wanted for a while, while not gratuitously inventing its own alien syntax.

One of the really neat things are multi-methods, which might sound dull, but actually address a real problem I've had in Java for a while: how to implement extensible "utility" methods that aren't logically part of a class. For exanple in Java you might do this (taken from this tutorial wiki):
package my.package;


import geometry.*;

class Tools
{
static boolean overlap(Shape s1, Shape s2)
{
if (s1 instanceof Rectangle && s2 instanceof Rectangle)
{
...
// return the result in this case
}
throw new Error("Case not supported: " + s1.getClass() + " and " + s2,getClass());
}
}
But in Nice you can do this:
package my.package;


import geometry;

boolean overlap(Shape s1, Shape s2);

overlap(Rectangle s1, Rectangle s2)
{
// Here s1 and s2 are both rectangles. We don't need to use instanceof or cast.
...
// return the result
}
Which is far neater and extensible: you can add new sorts of Shape and new versions of overlap (), which wouldn't be possible in the Java version.

0 Comments:

Post a Comment

<< Home