Menu

When all else fails, check the documentation

April 09, 2011

I ran into a bug in my code today because I didn’t implement clone in a class that needed it (it’s used for rescheduling a failed task). Naturally, I went to fix the code to make these errors impossible through the magic of Type Safety, but Java wasn’t playing fair. It turns out this doesn’t… Read more

Creating TypeLiterals in Scala

August 30, 2010

In case anyone wondered, here is how one can easily create instances of Google Guice’s TypeLiteral in Scala. Type literals are used for reifying types. def typeLiteralOf[T](implicit m: scala.reflect.Manifest[T]): TypeLiteral[T] = (m match { case m: ClassManifest[T] if m.typeArguments.isEmpty => TypeLiteral.get(m.erasure) case m: ClassManifest[T] => TypeLiteral.get(new ParameterizedTypeImpl(m.erasure, m.typeArguments.map { case n: ClassManifest[_] => typeLiteralOf(n).getType }.toArray))… Read more

I Can Has Invariant Mapz?

July 29, 2010

If I had to pick one of the major source of bugs in large refactorings I recently went through, it would probably be the bunch of methods in the java.util.Map interface which are contravariant on the key type. For instance, one can retrieve an element from a map using a supertype of its key type… Read more

Ids and Ambiguous Method Parameters

March 14, 2010

You’ve probably run into a method with a signature like: void addToGroup(long userId, long groupId) and a common question you might ask yourself is, “Oh, was it the user id first, or the group id first? I always forget. Stupid method.” You’ve encoded the meaning of the parameters into their names, and hence you need… Read more