Static typing (was Re: Java guy interested in Python)

Tim Peters tim.one at home.com
Sun Mar 11 13:16:34 EST 2001


[Rainer Deyke]
> When I type 'int i;' in C++, what I really mean is "'i' behaves like an
> integer variable.".

Since that's all "int i;" *can* mean in C++, seems tautological <wink>.

> I'm not saying anything about the values 'i' can take,
> except that the values must support integer semantics.

You're also explicitly agreeing to let the implementation decide which subset
of all the integers you're allowed to use, and are insisting on signed ints,
both of which say quite a bit about the values 'i' can take.  That this is
inadequate in practice is why C99 goes on to define a few dozen related
typdefs (int_least32_t, int_fast32_t, int32_t, etc).  It's still often (not
always) just approximating the set of values you really have in mind.

> The limited range of type 'int' is part of its defined semantics,
> as is its fast performance.

I expect you'll search in vain for a promise in the std that ints are "fast"
<wink>.

> If I want to restrict 'i' to a subset of its possible values, I use an
> assertion.

While if you were programming in Pascal instead, you could express subranges
directly in *its* type system; if you were programming in SLIM, you could
express concepts such as that a variable is restricted to even or prime
integer values; etc.  You use an assertion in C++ or *any other language*
because its type system isn't rich enough to capture all you want to say, not
because type systems are inherently incapable of capturing more than how big
machine registers are <0.5 wink>.

type-systems-are-to-types-as-4-bit-color-palettes-are-to-nature-ly
    y'rs  - tim





More information about the Python-list mailing list