Thursday, December 3, 2009

A silly trick for implementing parametrized traits

Here's something I noticed recently. In Scala, when you implement a generic trait, specialized to some particular type arguments, you end up having to rewrite the full method signatures, substituting in the type arguments. So, for instance, if you have

Kind of silly, but now the implementation is literally a copy/paste of the original trait, and you only have to provide implementations for the methods. In some ways, I think it's a little clearer than writing out the specialization.

It's too bad Scala forces you to repeat yourself though. The method signatures have already been supplied in the trait; unless you happen to be overloading method names, rewriting the method signatures provides no additional information. In effect, the problem of inferring these method signatures is already done. Other than not being valid syntax, it seems like I ought to be able to write something like:

You'd probably have to at least have the override keyword on the defs in the inheriting class to get around overloading ambiguity, and there'd obviously have to be additional restrictions (say, no overloads in the base trait with the same number of parameters) but it seems possible. A downside would be that when looking at the code, you'd also have to have the base trait API open to look at to figure out what was going on.

@Kris - Ah, yes, I suppose you do need to distinguish between the cases of defining a new, overloaded method with the same name as a base trait method, and implementing an existing base trait method. Although there is probably a way to infer that as well, without using a keyword and putting the onus on the user.

Also, the overrides aren't necessary in this particular case because there is no function definition to override (the trait doesn't define any functions). However, the compiler is flexible enough to allow the override keyword if you add it.

PS: Don't know why it won't accept the code html tag but I apologize for the formatting