[Types-sig] List of FOO
Martijn Faassen
m.faassen@vet.uu.nl
Tue, 14 Dec 1999 20:03:20 +0100
Guido van Rossum wrote:
>
> > From: Paul Prescod <paul@prescod.net>
>
> > > > #2. The first version of the system will not allow the use of types
> > > > that cannot be referred to as simple Python objects. In particular it
> > > > will not allow users to refer to things like "List of Integers" and
> > > > "Functions taking Integers as arguments and returning strings."
> > >
> > > It's been said before: that's a shame. Type inference is seriously
> > > hindered if it doesn't have such information. (Consider a loop over
> > > sys.argv; I want the checker to be able to assume that the items are
> > > strings.)
> >
> > It took two years to get the parameterized version of the Java type
> > system up and running.
>
> Probably because Java was initially conceived as a language with a
> "classic" type system (like C or Pascal). Python on the other hand
> already has all this.
>
> > Let me ask this your opinion on this question
> > (seriously, not sarcastically), should we include a spelling for "list
> > of string" and not "callable taking list of callables taking strings
> > returning integers returning string" and what about "callable taking
> > list of callables taking <T> and R returning list of callables taking
> > <R> and returning <T>." You see my problem? I could special case "list
> > of" as Java and C did if we agreed to take our chances that my syntax
> > would be extensible. We could even steal that weird "[]" thing that C
> > and Java do:
> >
> > StringType [] foo
>
> If we could express all those the type checker could do a much better
> job. If we could at least do the ones without the <T> notation, we'd
> still be doing a good job. Stopping at "list" is useless.
[snip]
I agree completely, and one *can* express most of this pretty easily in
current Python, i.e.:
types = {
"bar": IntType,
"baz": ListType(IntType),
"hey": IntType,
"foo3": FunctionType(args=(IntType,), result=IntType),
"crazy" : ListType(FunctionType(args=(ListType(IntType),
StringType), result=DictType(StringType,
FunctionType(args=None,
result=StringType)))
}
It looks very very ugly, but that's beside the point. It's usable for
type reasoning from within Python, directly (I actually have a buggy
module which this a little, and features typedefs to boot). One can come
up with a more Pythonic syntax (indentation, anyone?) later, once one
has the semantics working.
Regards,
Martijn