[Types-sig] Another cut at Sequence declarations

Tim Hochberg tim.hochberg@ieee.org
Thu, 15 Mar 2001 10:01:18 -0700


From: "Paul Prescod" <paulp@ActiveState.com>


> I think you're making good progress Tim! I'd appreciate it if you could
> finish the major protocols: Sequence (Mut/Unmu), Mapping (Mut/Unmu),
> ReadFile, WriteFile. I'll move the interfaces you define into my module
> and we'll put the base classes into some kind of basic types module for
> clarity.

I'll continue plugging away. Unsuprisingly Mutable sequences are turning
out to be more of pain than I had naively hoped. First off, the minimal
interface to simulate a list appears to be:

__getitem__, __len__, __setitem__, __delitem__ and either insert or
__setslice__

Or, I just realized, a __setitem__ that dealt correctly with slice objects
would also work. So,
eitther insert, __setslice, or __setitem__ (sometimes) work. Ick!

This makes it apparent that there are really two MutableSequences. A
MutableSequence and an ResizableMutableSequence. A Numeric array is an
example of the former, a list of the later. I'm not entirely convinced that
defining a mutable sequence type or types will turn out to be worth the
trouble, I think that most sequences passed to functions are used immutably.
I'll keep working on this though and see what I come up with.

Another issue. Anyone have any ideas on how to nondestructively test for
__setitem__? I have a few clunky ideas, but none that I like.

This brings me to my final observation: this would be immensely easier with
type-class unification. If every object implemented __setitem__, etc instead
of the tp_setitem it would help in two ways. First it would be trivial to
query whether a given object supported, e.g,m __setitem__. Second, it would
make it easy to define anonymous protocols. This would take a lot of
pressure of us to design a complete type system, we could focus on the
common cases. The above MutableSequence might be defined anonymously as:

def wibble(aMSeq : ('__getitem__', '__len__', '__setitem__')):

or maybe even:

def wibble(aMSeq: BasicSequence & ('__setitem__'))

OK, one more observation: it may be worth our while to encapulate whatever
we come up with for faking these checks as functions. In fact, I guess we
could just implement them as protocol checkers and then write the above as
either:

import typecheck as tp
def wibble(aMSeq : tp.BasicSequence & tp.__setitem__)

def wibble(aMSeq : tp.UnionOf(tp.BasicSequence, tp.__setitem__)


Now I'm starting to ramble, so I'll sign off.

-tim