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

Rainer Deyke root at rainerdeyke.com
Sun Mar 11 15:50:11 EST 2001


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.984334635.27345.python-list at python.org...
> [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>.

My mental model of what I'm trying to do is in terms of behavior, not sets
of values.  C++ merely reflects that.

> > 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.

'int' is basically short for 'int_fast16_t'.

> > 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>.

The intention, if not the specification, of 'int' is "the fastest
representation of an integer on the target machine that can respresent the
range from -(2**15) to 2**15, inclusive".

> > 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>.

Actually I could express all of these things in the type system of C++
through the use of template classes.  That would be a hack.  There are
assertion which cannot be built into the type system because they are
independent of type.  Given that, it makes no sense to try to model sets of
possible values through types.  In C++ (like in Python), types are intended
to express behavior.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list