[Types-sig] Static typing considered ... UGLY
Edward Welbourne
eddy@chaos.org.uk
Wed, 08 Dec 1999 18:39:26 +0000
Might I humbly suggest that:
to incorporate static typing into python would change it
beyond recognition
it would probably be better to start from Algol and pythonify it,
if that's where you want to go (hint: I don't)
the right name for the relevant language would be typhoon
because
it's almost an anagram
the real reason for doing it is <spit> speed
when it breaks things it won't half tear them into little pieces
? (albeit viper is already out there and doubtless good ;^)
A more pythonic approach would be to deploy some byte-code
hacks which notice assertions of form
assert isinstance(x, IntType)
and optimise ensuing code around the presumption that the value in
x when that assertion was executed is an int, allowing that all will
go horribly wrong if it isn't (which won't be checked unless __debug__),
but then we all know that speed kills. But only do this if the user has
asked for type-asserted enhancements, and use a different .pyc
extension for it. Might need a TypeException for throwing when it
all goes horribly wrong.
In a similar vein: could the interpreter and compiler exploit knowledge
of an assertion a function makes (about its return value) just before
returning ? i.e. calls to the function could presume the truth of what
the function asserted ... not that I'm convinced that this is worth it,
just that if you *insist* on static type notions, these are pythonic
ways to approach it.
But this is all `speed enhancement' (I refuse to call it optimisation:
I have no evidence it gets anywhere near the optimum). There is a
better way (I'll tell you about it late in January).
Note: I believe the function type() should be removed totally,
and isinstance should be replaced by (hint: think `type(x) in ...'
instead of `type(x) == ...')
def isinstance(x, *what):
"""True if x is an instance of any of the given types or classes."""
for mode in what:
if oldisinstance(x, mode): return 1
return 0
Then `try: ... except (tuple, of, exceptions):' would, of course,
be using the given tuple as *types when checking the exception raised.
I'd vote to keep this sig open for unification (all objects are objects
and support the same protocols - a module with __call__ in its namespace
is callable, for instance) but if all that's to be discussed is <spit again>
static typing, I'd vote for closure (prematurely and *with* prejudice).
I intend to follow up this bull-headedness in January.
See y'all at IPC8.
Eddy.
--
was it Sam Johnson who said something about knowledge of impending death
concentrating the mind ? Hence The Grim Guido re-woke the types-sig.