[Types-sig] Static typing: Towards closure?

scott scott@chronis.pobox.com
Wed, 19 Jan 2000 21:20:04 -0500


On Wed, Jan 19, 2000 at 12:22:12PM -0500, Guido van Rossum wrote:
> Here we see a tricky issue cropping up.  The links are declared to be 
> either a Tree node or None.  This means that whenever a link is 
> dereferenced, a check must be made.  The type inferencer thus must be 
> smart enough to detect these checks and notice that in the branch, the 
> tested variable has the more restricted type.  Most languages introduce 
> special syntax for this (e.g. Modula-3 uses the 'typecase' statement).  
> Can we get away with things like "if x is not None:" or the more 
> general "if isinstance(x, Tree)"?

I've actually spent a good deal of time pondering this issue and
coding segments that might potentially deal with having a type checker
understand idioms such as 'if x is None'.

These idioms are tricky for a number of reasons, that basically relate
to variations that are allowed in the grammar's definition of 'test'.

For example, I tend to use 'is None', your example uses 'is not None',
so the type checker would have to deal with understanding 'not' in
these idioms.  Furthermore, there's 'and' and 'or'.  When there's a
sequence of 'and's, we may assume that each condition is true for the
code block that is type-wise affected.  When an idiom is found in an
'or', things become complicated even more quickly.  Additionally,
there are those that use comparison (== 1, > 0) to guage boolean
values because of naivite or whatever.  Furthermore, there is the
issue of applying this check to types other than None.

In sum, it is clear that having a type checker understand certain
idioms can rapidly become extremely complex and win points with
confusing the user all at once.  Not a bad recipe for a massively
productive faq-populator :)

However, this case actually seems quite prevalent to me.  default
arguments and functions which either return None or something else are
quite common.  The frequency of usage makes me very much want whatever
syntactic construct is put in place to be as similar to existing idiom
as possible, both structurally and in terms of ease of use.

scott