J
John Roth
Harri Pesonen said:John said:Thanks guys, I hadn't heard of __slots__ before. Perhaps I am reading a
too old book. Of course, a compile time check would be better...
__slots__ came in with 2.2 new style classes, and it's definitely
an advanced feature. There's also some thought that it may not
be the best way to do the job.
I think that Python already has run time type checking. It does not
allow "a" + 1, for example.
Actually, it doesn't. It has run time strong typing, which is a different
thing.
To me, at least, "type checking" implies a meta-operation, that is,
something that is outside of the operation itself insuring that the correct
types are used. In languages with static typing, that something is the
compiler.
Runtime type checking means that there is additional code somewhere
that checks that the object you are trying to bind is of an allowable
type; that code is basically supervisory in nature.
It would be quite simple to have "option explicit" like in Visual Basic,
I believe. Like the following:
option explicit
var a = "asdf"
b = 1 # error at compile time
a = 123
(var b, var c, var d) = (1, 2, 3)
e = "asdf" # error at compile time
Things are not so simple. To do compile time type
checking, the compiler has to know the actual type
of the result of the expressions on the right hand side
of the assignments. In a dynamic language like Python,
that's not possible without very fundamental changes
to the nature of the language.
It's not a simple thing to do, and the fact that ML
family languages manage to do it without having
to have explicit type declarations (well, most of the
time anyway) is a major achievement, IMO.
The fact that Bicycle Repair Man manages to do it
as well as it does for Python is an even more
impressive achievement.
John Roth