A Nu way to reduce, reuse, and recycle code
Tim Burks, the author of [RubyObjC], has now publicly released Nu and here:
Nu is an interpreted object-oriented language. It uses a Lisp-style grammar, but is semantically closer to Ruby than to Lisp. Nu is written in Objective-C and is designed to take full advantange of the Objective-C runtime and the many mature class libraries written in Objective-C. Nu code can fully interoperate with code written in Objective-C; messages can be sent to and from objects with no concern for whether those messages are implemented in Objective-C or Nu.
The closest analogue to what Nu does is probably something like F-Script which is a Smalltalkish approach where Nu takes a Lispish approach. Nu being written in Objective-C has the feel of being on home turf where something like Ruby-Cocoa you feel a bit like an alien intruder.
Nu is still very new (the release is version 0.1) but I have been over the documentation, examples, and the interpreter source code I'm impressed with where it's at. The sample applications are a cool demo of Nu's capabilities although we're still waiting for the first public Nu application.
Here is an example (from Nu's stdlib) of some Nu code:
(class NSString
;; Create a copy of a string with leading whitespace removed.
(imethod (id) strip is
(set i 0)
(while (and (< i (self length))
(or (eq (self characterAtIndex:i) SPACE)
(eq (self characterAtIndex:i) TAB)))
(set i (+ i 1)))
(self substringFromIndex:i))
Although the syntax will be alien, Ruby programmers will instantly note "Ah, re-opening a class to add a method to it" and it's no surprise, given Tim's previous work, that Nu is heavily influenced by Ruby. Even to the extent of:
% (set laws "Asimov defined #{(+ 1 1 1)} laws of Robotics")
% (puts laws)
Asimov defined 3 laws of Robotics
Ruby-style string interpolation which is very nice. There is also a templating system in the distribution.
And of course Nu gives you the ability to reuse the entire Cocoa framework and any code written in Objective-C. That's quite a lot of re-use right there.
Should you use Nu? That depends on a lot of things.
First off Nu is interpreted. Tim's comments about "harnessing the power of C" were more about being able to leverage C so easily because Nu is based on Objective-C which is based on C. They weren't meant to imply C level performance. Indeed, as of now, I'd expect Nu to be comparitively slow both because it's interpreted and because it's immature.
Then there are the (((((()))))). I'm editing Nu code in TextMate and, while Nu does come with a bundle which includes a reformat command I find it an akward environment to write Nu code in. Maybe it will grow on me. If you are allergic to brackets then Nu probably isn't for you.
And Nu is, well..., new. We're already discussing the syntax on the list and an example of the kind of issue to be considered was raised on Reddit yesterday:
(array count)
This calls the count method on the array object.
That is: the function being called is the second argument.
WHAT THE HELL?
I know what he's trying to do:
he's trying to take Obj-C's message-call syntax of
[object name: argument]
And shoehorn this into an s-expression format.
Nu's attempt to be "Lisp re-interpreted in the context of Objective-C" is going to throw these things up. Only time will tell whether the choices Tim has made have been the right ones and how the language will evolve in response to wider use and feedback.
This is one of the exciting things about a new language but I guess it also means you should expect a bumpy ride.
Although I'm pushed for time to work on it right now I am planning my next Cocoa application. I had intended to use Ruby-Cocoa to add a layer of interpreted goodness but I'm now rethinking that in terms of an Objective-C core enhanced by Nu.
(p.s. I'm hanging around in #nu on irc.freenode.net if anyone wants to chat about Nu).
