Tag Archives: london

No, seriously, why Scala?

Recently an article called Why Scala? was posted on reddit. It’s an ok introduction to the language, but the very fair observation was made that it’s much more of a “What is Scala?” than a “Why Scala?”. I thought I’d share my thoughts on the subject. Mainly because I like hearing (reading) myself talk (write). :-)

Quick background: I initially learned to program in standard ML while at university (self taught, mostly, with the help of some friends doing computer science. I was doing maths). On graduating I then switched tracks entirely and started doing Java web development in a small software firm in London (I’ve since switched again, but I’m still doing Java professionally). I’ve also dabbled and read a lot with computer science and programming languages in my spare time since then, filling in the gaps that not having done any real computer science at university left me.

My train of thought on the language switch from ML to Java was basically:

a) Wow, this is different.
b) Ugh. Where are my higher order functions?
c) Where are all these random exceptions coming from?? I’ve compiled the code successfully, isn’t it supposed to work now?
d) Hmm. But there’s some useful stuff here too.

Scala’s a nice way to scratch both itches, and adds some very interesting features and functionality of its own. It’s not my favourite language (I don’t really have one. All languages suck. It’s just that some of them suck less in interesting ways), but it has a lot I like. Here’s a brain dump of some of it.

Things I like:

Object oriented programming

I know, it’s so entrenched it’s past even bothering with its buzzword status. But object oriented programming has a lot of advantages for medium to large scale composition. It has some disadvantages, and frankly sucks at small scale composition of functionality (which is where functional programming shines), but it allows for some very nice pluggability.

Module oriented programming

ML has higher order modules. I never really used them much when I was programming it more often (mostly because I was only writing enough code to do some simple maths projects. I never wrote anything large scale), but having looked into them in more details since they’re really powerful. They’re essentially a different take on the composition that object orientation provides. Where object orientation resolves everything dynamically, ML’s higher order modules resolve everything statically. This introduces some limitations in flexibility but makes up for them in power and type safety – they provide a much more flexible and interesting abstraction over a type than mere subclassing and interfaces can.

Scala has both. Further, it has both and lo and behold they are the same thing. Objects are modules, and can declare their own types (note: This is much more than just declaring an inner class in Java is), imported, etc. Modules are objects and can be instantiated at runtime, extended, etc. You lose a bit of the static guarantees that ML modules but you gain a lot of flexibility from both sides.

Static Typing

I’ve written too much Java to not like static typing.

Wait, I know that sounds like a non sequitur, but read on.

I’ve written too much Java and seen flagrantly stupid and really subtle runtime errors that should never have made it past the compiler coming out of it to not like static typing. NullPointerException, ClassCastException, argh.

If you’ve written enough code in a language like ML, OCaml or Haskell you will know that the compiler is your friend. And, like all good friends, it will yell at you if you do something stupid and then help you pick up the pieces.

Scala doesn’t quite manage that. If you write code in just the right way you can achieve that level of guarantee (and in some cases, more. But that tends to be the result of abuse of the type system by deranged maniacs), but the combination of subtyping and some Java interoperability decisions mean that it’s not quite as good. It’s not bad though.

So: I like object oriented programming, I like static typing. It logically follows that I must like statically typed object oriented languages, right? Well, in principle, yes. But Scala is the first one I’ve met with a type system that didn’t suck. Scala’s traits (a sort of mixin) are so much better to work with than interfaces, the generics work properly, provide variance annotations, etc. A reasonable subset of the types are inferred. Compared to the type systems of Java, C# and C++ it’s a dream (it’s not as nice as the type systems of the statically typed functional languages I know of. Subtyping seems to cause issues, with a lot of research still needed to make it work well, and Scala seems to have largely ignored what prior work there was Hindley-Milner style type systems with subtyping)

Functional programming

You’ve all been dreading this section. “Oh no. Now he’s going to enthuse about how marvelous functional programming is and how it’s going to cure cancer”. Nope. Can’t be bothered. Functional programming is nice. If you don’t believe that, I’m not going to try to convince you of it. Scala’s support for functional programming is ok. It has some warts, but it also has some nice points, and it generally works well and isn’t too verbose. I’m not going to get any more excited about its presence than I am about the fact that my bike has wheels (but I’d be pretty pissed off if my bike didn’t have wheels). Higher order functions, pattern matching, etc. It’s all there. It works. Moving on swiftly…


Scala offers a bag of features under the keyword ‘implicit’. This is one of those things that makes you go “Oh, that’s cute” when you first see it and then go “Wow, that’s powerful” six months later.

Essentially implicits give you statically guaranteed and provided dynamic scoping. You say “I need a Foo. I don’t care where it comes from”, the compiler says “Here you go” or “Sorry, no Foos today”. These can be objects, implicit conversions between types (You know the way Ints get implicitly converted to longs, double, etc in Java? Scala does that too, but it’s all programmer definable. They’re just library functions in scala.Predefined). If you remember what I said about Scala objects being modules and you’ve read this paper a little light might just have gone on in your brain. If you haven’t read it and don’t want to, here’s the summary version: Implicit function arguments + first class modules gives you something that looks and quacks very much like Haskell type classes (yes, I know this isn’t actually what the paper says, but it follows from it). Mmm.

These are the big things to like about Scala. Here are a few little things:

  • Sane constructor/class semantics. If you’ve written a lot of Java there’s a good chance you hate its constructor system. Scala’s is much nicer.
  • Expression oriented code. Everything is an expression. You can form compound expressions trivially – { val foo = bar(); baz(foo, foo); } is an expression which evaluates to baz(foo, foo).
  • Sanely uniform scope. Pretty much anything you can do inside a method you can do inside an object and vice versa. Things are for the most part lexically scoped in the right way.
  • The primitive/object divide is much less irritating. Primitives get a few special treatments at the language level, but mostly they’re just objects. When things should compile to use primitives, they do. When the primitives need to be boxed, they will be. It’s almost entirely transparent.
  • Performance. Scala generates very good (well. ‘good’. Java-like) bytecode, which means it gets to take advantage of most of the optimizations the JVM is willing to throw its way. Further it puts a reasonable amount of its own work into performing optimisations on the bytecode, etc so you get those nice juicy abstractions without much overhead. There’s essentiall y no performance penalty for choosing Scala over Java


Scala’s far from perfect. It has some syntactic weirdnesses, a few issues carried over from Java, a moderately buggy compiler and a host of little features and edge cases that are really hard to keep in your head. However, I find that these issues don’t actually do more than annoy you from time to time. The core language is powerful and very useful for just sitting down and writing good code in.

Birds of a Feather and AMQP

Minor announcement in case anyone who lives in London actually reads this thing. :-)

Alexis mentioned to me at wednesday’s London HUG that he’s giving a Birds of a Feather talk on AMQP at No Fluff Just Stuff next week. It looks quite interesting.

If you don’t know, AMQP is a newish protocol for application level messaging. One of the big implementations out there (which is the one Alexis is involved with) is RabbitMQ, which is written in Erlang (which is how I got interested in the subject in the first place). By the sounds of it, they’ve been doing some quite fun and useful things with it. This should definitely be an interesting talk and I highly recommend coming if you’re in the area.

This entry was posted in programming and tagged on by .

Cooking lessons 3

This is going to get hopelessly out of order very quickly, as there are now two lessons I’ve missed out on actually posting. Never mind.

Yesterday I took Michael spice shopping. I was actually a bit unimpressed with where we went, so if anyone can tell me some good places to go spice shopping in London I’ll be really grateful. We bought ground cumin, ground red chilli, garam masala and (gasp) curry powder. I went for all powders because they’re a bit easier to work with to start with. Also because the selection of whole spices was crap. We can get on to the subtleties of spice usage once we’ve covered the basics, and once I’ve found a decent place to go shopping for them.

Here’s one of the first Indian recipes I ever learned to cook, about four years back when I was first trying out this ‘vegetarian’ thing and Tariq came into our kitchen and found my attempts at making a curry. They were umm… not very impressive. Ask nicely and I’ll tell you about it some time. Here is a very simplified and tinkered with version of the recipe he showed me.


  • One small bowl of lentils (about a serving size in order to make two to three servings). Preferably green, brown, puy, etc. Black or red won’t really work here.
  • One medium-large onion.
  • Sunflower oil.
  • One large spoon of ground cumin.
  • One spoon of garam masala.
  • A quarter of a spoon of curry powder.
  • Water.
  • Salt to taste.


Mix the dried spices and salt. Dry fry them on a medium heat for about a minute, stirring constantly, and then transfer to a bowl.

Add enough oil to the bottom of the pan to just cover it. Dice the onion and fry it on medium heat until soft, and then add the spices. Fry for a few minutes more.

Now add the lentils and fry for about a minute. Cover with boiling water (say two to three times as much water as you had lentils) and bring to a boil. Reduce the heat to simmer, cover the pot, and leave it.

It will probably take about half an hour to cook. Be more worried about undercooking it than overcooking, but check on it every now and then to see if it’s getting dry and needs more water. The end result can either be dryish or soupy as you prefer. If it’s going to be soupy you might want to consider adding more salt and/or (horrors) half a stock cube. When the lentils are soft to eat it is ready.

This entry was posted in Food and tagged , on by .

Cooking lessons 1

A few months ago I was walking through London with a friend of mine. For the sake of the argument let’s call him something implausible like Michael. In the course of conversation two things came up. Firstly, that he was very low on money. Secondly, that he wanted to go to McDonalds because he could get a meal’s worth of food for only five pounds there.

Needless to say I objected rather strongly to this statement. Both to the notion that one can acquire food at McDonalds and to the notion that five pounds for a meal is good value. And so it arised that I would be teaching Michael how to cook.

Michael has now returned from the barbarian lands which he calls home, and so the lessons are to begin. Because it will allow others to benefit from them, and because I’m a total show off, I’ll be doing it via a series of blog posts.

Today is shopping day, and I’m suggesting a list of bare minimals he’ll want to stock before we do this. Spices will come later, as I refuse to instruct anyone to buy spices at a supermarket.

Cooking implements and general kitchen stuff

He actually has most of these, but I’m including it for completeness. Some of these aren’t essential, and one can always improvise, but it’s irritating to have to do so.

  • Cutting board
  • Sharp knife
  • Frying pan
  • Pot (Having two pots is ideal, but not neccesary)
  • Wooden spoon
  • Cooking spatula.
  • Large sandwich bags
  • Cheese grater
  • Aluminium foil

Cooking essentials

These are the ingredients which I feel it would be useful to always have to hand.

  • Sunflower oil. This can be as cheap as you can find.
  • Garlic puree
  • “Very lazy chillies”
  • Table salt
  • A couple packs of green, brown and red lentils respectively
  • A couple bottles of tomato passata.
  • White rice. Preferably basmati.
  • Stock cubes. Something of midrange quality is likely fine.
  • Bag of cheap white onions.
  • Bag of potatoes
  • Marmite
  • Soy sauce
  • Sugar, preferably brown

Not all of these are things I would use. I’ve replaced some of my ingredients with equivalent shortcuts.

Short term stuff

Things which I’d recommend picking up in the short term. This is definitely not a required list, but will give rise to some nice easy starting meals.

  • Eggs
  • Carrots
  • Cucumber
  • Fresh fish from the fish counter – if you buy whole fish rather than steak you can find some quite reasonably priced examples.

Other stuff

I like to have the following around, but it’s totally nonessential.

  • Balsamic vinegar
  • Sesame oil
  • Miso (if you get the fish I recommend picking up some of this)
  • Sweet chilli sauce
This entry was posted in Food and tagged , , on by .