Registered: Apr, 2005
Re: Is Static Typing a Form of Bad Coupling?
Posted: Apr 13, 2006 2:08 PM
> > So in the simplest case, you could annotate a parameter
> > as an int, but go ahead an pass a string anyhow. The
> > annotation would have no more effect than a comment.
> > (Presumably, there would be standard decorators that
> > actually did something, but that isn't fleshed out at
> > the moment.)
> OK. It sounds like your approach in Python is consistent
> Gilad Bracha's notion that optional types shouldn't affect
> the runtime semantics.
The types themselves, yes. But the decorators certainly could. A decorator could raise an exception if the type wasn't correct, or it could try to adapt the argument so that it is correct.
My personal view is that some of this ought to move to compile-time and not require a decorator. The psyco extension already does this (with type inference, rather than introspection on type specifiers) and provides a fallback for cases when it is wrong; effectively it compiles to a fast path and a safe path, with an extra case statement to choose between them.
Guido is still leaning towards the annotation not meaning anything in the absense of a decorator, because he doesn't want to close off any avenues. (The multiple pluggable types that Gilad discusses.)
> I also think that if people end up
> doing runtime type checks a lot in Python as systems grow
> in complexity, then having a shorthand way to specify the
> types and indicate you want a decorator to check them
> dynamically would be useful.
Yes, but I hope that doesn't happen. Realistically, most typechecks in python are bugs, because they end up excluding too much. (They check for a string of text, but don't get updated to allow unicode, or they check for an integer, but don't allow a gmpy.mpz.)
If you pass something that really won't work, you already get a TypeError (or an AttributeError), when you try to use it.
In theory, catching these at compile time instead of waiting for run-time should still be a huge gain. In practice, when I've compared my own python to my own java, the type safety didn't actually buy me anything. Usually, the code actually did work in python, and when it didn't, the problem was generally a RunTimeError even in java. (Usually a NullPointerException, rarely a ClassCastException.)
> What my imagination is struggling with is how much value
> optional type annotations could give you for static
> analysis of the program. Are there other benefits you hope
> that optional type annotations would provide in Python
> besides the easier dynamic checking one?
A breakthrough for me was replacing "type specifier" with "parameter annotation". It is useful in pretty much the exact same ways as introspection is generally useful.
> Oh, I guess explicitness of the code when programmers
> are reading it is one.
Readability is probably the most important reason, but whether annotations (particularly type specifiers, which might cease to be optional in many organizations) would really help or actually hinder (because of too much boilerplate) ... I'm not sure.