So, recently I’ve been working on a rather gargantuan project in PHP and I keep thinking, “Man, I wish this were a statically typed language.” This might seem strange because I have so often been a proponent of scripting languages, most of which are far from statically typed. (PHP and Python let you re-define an object’s class dynamically if you want to (terrible idea) and I recently posted how you can create a class dynamically in PHP (and that is great for testing). Because it is so big, I’m actually using a proper IDE, specifically Eclipse[1].
Eclipse, it seems, has started to get PHP right. The class name, function name, and method hints are working as expected. Documentation pops up and is generally fairly useful. And life is good… mostly.
There is a problem though, something which would not be there if I were using something with a clear, static type system. The problem is that hinting only goes so far. While in, say, Java, Eclipse can read that foo.bar().baz().bat() will return a “widget object”, in PHP the IDE can make no guarantee that $foo->bar() will return anything.
It was said at one point that for proper type-hinting to exist, you basically need some form of language interpreter, something which actually has a knowledge of the structure of the language, and this is more true of the dynamically typed languages than it is of the statically typed languages.
Think about it. For a Java IDE to provide hinting for foo.bar().baz();, it has to say: “OK, Foo.bar returns a Bar object. Bar.baz returns a Baz object. Baz has methods X, Y, and Z…” while PHP (and Python to a slightly lesser extent), on the other hand it has to be, “$foo->bar() returns… something. Ok, I’ll look into Foo::bar. Foo::bar seems like it is always returning a Bar object, what is Bar::baz? Hm… Bar::baz returns $this->baz_instance; Where can I find the definition of that? Hmm…” And, at that point, it is quite possible that it isn’t possible to answer that question.
With the mantra of “pragmatism is the greatest tool of the developer,” I do really wonder if all of the wondrous possibilities which are offered by list comprehensions and immediately available reflection are worth it. Maybe Gosling was right in saying that dynamic typing makes it more difficult to build larger structures. After all, if the tools for a language are inadequate, then the language has no business in business — if it fails pragmatism, it simply fails.
So, perhaps the best argument for static typing has not come from a person, at least not directly. I’ve read the greats (in this case, Gosling and Van Rossum and their followers), but neither of them really made a compelling argument which did not boil down to “I like this type of coding better”. On the other hand, there is my IDE which is stating plainly and clearly, “I am too stupid to handle this language well.” And I, for one, find that a compelling argument indeed.
- [1] I am not specifically endorsing Eclipse over Netbeans, but I can do both Flex and Android easily in Eclipse and I can’t do those in Netbeans. That and Netbeans is owned by Oracle, and I don’t like them. ↩
Yeah, I hear you. Even a fake static typing, like in ActionScript 2, would be really nice for things like this. Or have lazy optional static typing. If you define it:
function foo( $bar ) {
return $bar * 10;
}
It’ll provide no hinting, but if you could optionally define it thus:
function foo( $bar:int ):int {
return $bar * 10;
}
Or something, it would give you the proper hints.