Adding static typing to Python
ajeru at vknn.org
Tue Feb 19 19:58:10 EST 2002
QnickQm at alum.mit.edu (Nick Mathewson) wrote in message news:<slrna74ssf.21s.QnickQm at localhost.localdomain>...
> In article <24c39b2c.0202181531.187fad4c at posting.google.com>,
> Alexander Jerusalem wrote:
> > Has anyone heard of plans to add static type checking to Python?
> Someone else has mentioned an old proposal of Guido's for adding
> optional type declarations in order to help the compiler catch errors.
> Making a real static type system is harder than you'd think.
> Consider this function:
> def fn(lst,item):
> for i in lst:
> if i == item:
> return repr(i)
> What is the type of fn? You could call it:
> (list[int], int) -> str
> But then you'd lose the polymorphism of the function. To a first
> approximation, you might say:
> (list[A], A) -> str
> But keep in mind that not any A will work...
> (list[A], A) -> str s.t. A has __eq__(A) and __repr__() -> str
> And that x==y is sometimes okay even if x.__class__ != y.__class__...
> (list[A], B) -> str s.t. A has __eq__(B) and __repr__() -> str
> But there's nothing that says you can't use a tuple or a string or a UserList
> instead of a list...
> (sequence[A], B) -> str s.t. A has __eq__(B) and __repr__() -> str
> And you aren't using _all_ of the sequence protocol: just iteration...
> (A, B) -> str s.t. A has __iter()__ -> C
> s.t. C has next() -> D (or raises StopIteration)
> s.t. D has __eq__(B) and repr() -> str
> IMNSHO, some kind type inference is the only tractable answer.
> And-what-if-repr-doesn't-return-a-string?-ly yrs,
You argue that there are cases where static typing gets in your way,
where it makes programs less flexible. That's true and that's the
reason why I my suggestion was to add it as an optional feature. It's
also clear that it's not easy to do and that you need generics if you
have types otherwise you get what Java has: ClassCastException.
I like multiple type based dispatching (multi methods) (not
inheritance based dispatching) and I don't see how this is possible
without declaring types although someone has suggested that CLOS does
it without static typing. I'm not sure how this is possible or if this
means that no type declarations are necessary.
More information about the Python-list