Tuesday, November 27, 2012

No Function.apply, but Wins Still to Be Had

I prettied up the Dart web server code in my Dart Comics sample app quite a bit recently. As pretty as it is, I have been lamenting the lack of a splat operator or an apply method for functions to make things even prettier. It turns out that there is, in fact, a Function.apply, so let's see if that will help.

The first thing that I notice is that it is a static method, so something like this is probably not going to work:

The problem here is that if I delete the first record in the database then db.length + 1 now refers to the last record in the DB, not the next available ID.

The way that I got around this in the node.js version of the application was to make use of node-dirty-uuid. In Dart, I am going to give dart-uuid a try. Since this is Dart and since dart-uuid is available in Dart Pub, trying this out is a simple matter of adding dart-uuid to my application's pubspec dependencies:

name: Dart Comics
dependencies:
dirty: any
uuid: any

After a simple pub install, I am ready to go.

And it turns out to be as simple as importing dart-uuid, instantiating a UUID object, and using it: