Parameterized Types
Roy Katz
katz at Glue.umd.edu
Mon Jan 15 10:40:49 EST 2001
Hello,
<asbestos-cape="if you respond, be nice">
I came across http://www.python.org/~guido/static-typing/ the other day.
Some things that struck me as odd:
- parameterized types:
class foo<TYPE>: pass
def func<TYPE>: pass
Why is this necessary? I think the point of parameterized types is
to tell the Python compiler that we are only going to use TYPE and
TYPE. Is this correct?
Which begs the question: is it possible to write an analyzing
tracer which traces the types of objects and generates efficient code?
for example -- the gcd() function:
def gcd(a,b):
if a>b: return a
return b
Now, if in my program all I pass to gcd() are int's, the tracer would
recognize this. So I don't need any decl statement; the compiler "knows"
that I will only pass in int's. Presumably, no optimizations would be
made if my script passes in a lone string or makes a decision between
passing a string or passing an int, etc.
- Syntax for static typing
The suggestion
decl f(x: int) -> int
or
decl f(x: int) -> [int]
looks to me like a mathematical mapping. Is there any cleaner way to
specify this? instead of ->, can't we overload an existing keyword
('return')? Similarly, [int] looks like 'list of integers of size
1'. The slides expressed inlining and explicit forms of declarations;
is it possible to come to a concensus before TMTOWTDI?
For that matter, is -> or return even needed?
def f(x: int) int:
pass
I use Python for scripting and larger programs (RPGs, etc.).
What I need in static typing is this:
+ Clean and unambiguous
+ No new keywords ('->' vs. 'return')
+ There's only *one* (right) way to do it.
I pondered whether it is important to be able to write class/func
prototypes separately from implementations -- but then that we get
the following:
decl func( x: int ) -> int
def func(x)
pass
or
decl func( x: int ) -> int
def func(x: int ) -> int:
pass
I like the second approach better because it is more consistent
(imho) between declaration & implementation. Then again..
with the first approach, if you take out the decl statement, then
func becomes fully generic. In the second implementation, func
stays statically typed even when we remove the decl statement.
An additional concern is that Python will become
like Pascal in the sense that it is very readable, but it just
takes too long to write out (relative to C/perl/etc.)
</asbestos-cape>
Roey Katz
president,
Federation of Ignorant Scripters.
More information about the Python-list
mailing list