Monday, March 3, 2008

Generics in Javascript

Apparently, there are talks about adding generics to Javascript.

I'm not sure why people would want generics in a dynamically-typed language.

Personally, I like:

var EmpList = []
over
var EmpList:List.<Employee> = new List.<Employee>();

The one thing I really dislike about static typed languages is this naive assumption that a language should restrict the "damage" a programmer can do. I think that's just plain bad design, because people end up relying on the software when it's really the programmer's job to unit-test their code in the first place.

Making generics invariant is just silly. In the EmpList example, writing

EmpList[0].bark()
is going to throw errors regardless of whether you have type constraints or not. This whole thing about object sniffing has been discussed enough times already and this sounds like a pompous version of the same thing. When you have no control over the environment where your code will run (e.g. a browser), you can't just go and do
var a = new List.<Array>([]);
a[0].forEach(doSomething);

At least they're suggesting an unambiguous token (".<"), instead of copying Java literally, so implementation should be a little less hellish, and thus, a little faster to parse through.

No comments:

Post a Comment