We don’t need anonymous inner classes? Bollocks to that.

Robert Fischer over at enfranchisedmind.com has a post about anonymous inner classes which I am forced to disagree with quite strongly.

He starts off with the example of a comparator, and shows that defining a lambda and then casting it to a Comparator is terser. I largely agree. I don’t agree with everything about that example, but it would be churlish to quibble over details. I concede the point: For one method anonymous inner classes, lambdas are better.

He then attempts to generalise this to anonymous inner classes with multiple methods.

Except he doesn’t really. He simply states that because Groovy lets you coerce maps to an interface, you don’t need anonymous inner classes.

This is an interesting point. Let’s take it further.

We can define the following function (in some sort of pseudo syntax because I don’t know Groovy):

defineClass("Kitten", {
  "meow" => () -> println("meow"),
  "play" => it -> ...
})

Hopefully it will be taken as uncontroversial that I don’t consider this to be the best idea I’ve ever heard. There’s certainly a place for things like this – e.g. if you’re starting from a prototype based language like Javascript and want to build a class system on top of it, something like the above makes sense. But with classes baked into the language, I much prefer

class Kitten{
   def meow = println("meow");
   def play(it) = ...
}

It’s cleaner and terser syntax, and it better expresses the meaning.

So. We now have two ways of expressing classes which map to our two ways of expressing anonymous inner classes. Except that for some reason we prefer one in the named class case and one in the anonymous inner class case. This is odd, to say the least.

Now I want to pick a bone with the claim that what we’re really doing when we create anonymous inner classes is passing multiple functions. It seems utterly contrary to reality. That may be true in some cases when you’re using them as an argument to some method, but most languages I know have this interesting feature called “returning values from functions”. I know you don’t see it getting used much what with the current fad for writing all your programs in continuation passing style, but it does happen occasionally.

Consider example the following implementation:

class Vector[T]{
   ...
   def elements = new Iterator[T]{
     var position = 0;
     def hasNext = position < length;
     def next = {val it = this(position); position += 1; it }
   }
}

This pretty unambiguously captures the intent: We’re creating a new iterator which captures the local scope. The alternative version (again, pseudo syntax):

class Vector[T]{
   ...
   def elements = {
     var position = 0;
     { 
       "hasNext" => () -> position < length;
        "next" => () -> {val it = this(position); position += 1; it }
     } as Iterator
   }
}

has absolutely no appeal to me. It muddies the intent and has worse syntax (if someone from the groovy world would like to tell me that the actual syntax for the above is better, please do, but I suspect I’ll still dislike even a slightly tider version of the above).

Iterators are one example, but there are plenty of others. e.g. in SBinary, formats have two methods read and write, and I return new ones as anonymous inner classes all the time. It would have been a maddening library to write without the capability to do that.

Anonymous inner classes get a bad reputation because so often their use is as a poor substitute for first class functions. But they really are a practically useful tool. I have plenty of other ideological reasons why I consider them vital to have, and this article was originally going to contain some of those, but I couldn’t be bothered. So I’m going to leave it at that: They’re useful, and they cleanly capture the intent in a way that I feel that coercing a map does not.

This entry was posted in programming on by .

Planet Scala gets its own domain

It can now be found at http://planetscala.com/

The old location and feeds will continue to work for the foreseeable future, but I’d appreciate it if people were to start using the new URL instead.

This entry was posted in Admin, programming, Science, Uncategorized and tagged on by .

Filtered feed to Planet Scala

I’ve just noise reduced the first feed on Planet Scala, as it really had a bit too much totally irrelevant stuff on it.

Specifically, mine. :-) You guys don’t really need the feed spammed when I decide to write fiction, recipes or whatever. This blog is about a bit too many things, and only a subset are relevant. So I’ve cut it down to only publish the programming related posts.

As always, do let me know if you think there are any other feeds that should be treated similarly.

This entry was posted in Admin, programming on by .

How do you talk about Scala?

I gave a talk about Scala at Last.fm last night (It’s not online: not on Last.fm. At. I physically walked over to their offices and gave the talk to some of their devs).

Depending on how you look at it, it either went moderately well or was a complete disaster. People seemed interested, and went away knowing more about Scala than they came in with, so that part went well. The big issue is that I never actually talked about the subject I went in prepared to talk about. Oops.

I think part of the problem is that I aimed the talk at slightly too high a level. The talk I had designed was about testing with ScalaCheck, as that’s a pretty nice distinguisher of Scala from most of the other JVM languages – it’s something that really takes advantage of the type system in powerful ways, but isn’t too scary. I went in with a set of code I’d already written and was going to live code the tests for it. I still think this would have been a good talk, but I think it would have been a much better second talk on Scala than an introduction.

What happened instead was that we very quickly got diverted onto basic questions of syntax and semantics, and ended up touring Scala through the interpreter and performing a sort of general Q&A about it. This worked reasonably well (at least partly because Miles joined in and backed me up on some of the questions. Thanks, Miles), and would probably have worked even better if I’d come in with a couple small examples to demo this way. So maybe there’s something worth refining in there – certainly the “Here’s the interpreter. Let’s talk about code” model seemed like a pleasant one.

But I feel the talk still sortof missed the mark. It wasn’t a bad introduction to Scala, but it wasn’t a good “Here’s why you should use Scala”. And I’m still not sure how to do one.

The traditional way to present Scala seems to be to present it as a better Java. “Here’s an example in Java. Let’s translate it to Scala. Oooh, look how much shorter it is!”. This seems to go pretty well from what I’ve heard reported. But I have a couple problems with it: Some philosophical, some practical.

The biggest one is that the JVM is not short of languages which are better than Java. I don’t consider the question “Should I use Scala instead of Java” interesting. The answer, as far as I’m concerned, is obviously YES. Except for a few remaining interoperability issues I don’t consider that there’s any good reason to use Java these days (Disclaimer: The one exception I grant is in creating other JVM languages which you don’t want to be burdened with the Scala standard library on top of your language’s runtime). The interesting questions are “Should I use Scala instead of Clojure?” or “Should I use Scala instead of JRuby?”.

And here’s the thing: Rewriting a simple Java example in a better language looks exactly the same with almost any nicer language. You do a straight port, you find opportunities to introduce some functional programming, maybe you take advantage of sequence comprehensions or such like, etc. but basically what you end up with is the original program with a terser syntax and a few obvious abstractions factored in. It’s really not that exciting, and it doesn’t sell the language well. It might, if you’re talking to a group that has no experience of other languages (I wasn’t), but even then you’re basically playing on your group’s ignorance rather than making a compelling argument.

There are a bunch of neat things in Scala that could be used as a compelling argument: Scalacheck, actors, parser combinators, pattern matching in general, etc. but I’m not really how to go from zero to a full fledged example in one of these in only an hour. Perhaps if (rather than starting from a blank slate like I did) I started from a pre written example and disected it, but I’ve no idea if that would work any better.

So, what have you done? Did it work? Any ideas for how to do it better?

This entry was posted in programming and tagged on by .

Spam

I recieved the strangest message earlier.

“HOt SlutS will be begg1nG for your cock!!!”

Well, yes. I suppose they could be. But it would be a bit childish, don’t you think?

Anyway, I tracked it down to an old archive machine. It seems it had been purely in monitor mode, but through some bug or malice had started forwarding messages from its data source. Rather odd ones, but that in itself wasn’t so remarkable.

Still, it was curious, and I had a few minutes spare time, so I thought I’d investigate further.

It seems it was a very old archive machine, hooked up to the most marvelously archaic setup. A few million ancient computers scattered across the planet, all connected up together with fiber optics. They must have been decades old. I’d never seen the like.

It seemed mostly to be concerned with sending text like the above amongst itself. There were a couple common variations – something to do with “viagra”  or “cialis” (perhaps they were the same thing?) appeared to be the most common theme, but “amateurs” featured frequently as well.

Each piece of text would be sent from one machine to many others. Sometimes it would stop there, sometimes it would be forwarded to another. Occasionally the same mail would be passed on to further recipients. More often it would simply sit there, eventually getting deleted.

It was all very peculiar. I couldn’t quite see the point of it to be honest. Clearly some sort of laboratory experiment someone left running and forgot about. I suppose it might be interesting to some people – there’s no accounting for taste.

Anyway, I took a backup of the whole thing and then shut it down.

It wouldn’t do to let this sort of thing get out of hand after all.

This entry was posted in Fiction on by .