I read my yesterday responses and they seem quite chaotic. Hopefully this post will make things more clear.
I never said you cannot do something in statically typed language. You can always (?) achieve the same effect by getting rid of the type system, e.g. using void* and/or type cast, adding runtime checks. After all, the Python interpreter is implemented in C. It’s just a matter of how easy and natural it is.
I was mistaken by linking some language features to dynamic typing. They are independent in principle, it’s just that they happen to be found much more often in dynamic languages.
I was also not aware of the possibilities of the type system in some less mainstream statically typed languages. I guess it’s not the static typing that sucks, it’s the design of the type system in some languages. I should have been more specific. A critique that is valid for a particular language X is not necessarily valid for statically typed languages in general.
I also look at things from a very practical point of view. Back to the example with extension language - it’s true that extending C++ with Lua is equivalent to using Python for both the application and extension language, it’s just that the latter so much easier. Not in theory, but in reality.
So, thanks for the discussion. Finally, I have a small exercise for rapidcoder ;)
>>> class Foo:
def doit(self, a, b):
return (a + b)
>>> foo = Foo()
>>> foo.doit(10, 20)
30
>>> Foo.doit = lambda self, s: s.upper()
>>> foo.doit('abc')
'ABC' |
EDIT: Changed the example so that the new method expects different types of arguments. The first version didn't illustrate the point well. And the point is that there's a limit on what you can statically analyze. To do certain things you need to widen your static type definitions so much it's hardly static typing any more. Theoretically the compiler could know when the type changes, and verify that before and after this happens the method is called with the right types of arguments. But it's not easy to do and thus statically typed languages either limit what you can do, or give you ways to escape the type system.