strong/weak typing and pointers

Mike Meyer mwm at mired.org
Wed Nov 3 02:03:33 EST 2004


Steven Bethard <steven.bethard at gmail.com> writes:
> Don't get me wrong -- I do understand your point.  In every case I can think of,
> there is no reason to want weak-typing (PL theory definition) in a
> dynamically-typed language.  On the other hand, I haven't really seen any good
> cases for wanting weak-typing in a statically-typed language either.

First of all, let's mention a truly weakly-typed language. BCPL, one
of C's predecessors. Variables don't have types, they just hold
words. Operations treat those variables as having a different type:
adding them as ints, or adding them as floats. Dereferencing them to
arrive at another word. Subroutines were just variables that held a
pointer to code, and people would actually write code that looked like
(pseudo-c):

func() {
       init_func ;
       func = foo ;
foo:   func_code ;
       }

To only run the initialization code the first time the function was
invoked, but not any other times. Of course, there was an external
program (written in BCPL) that did type inferencing, and would warn
you when you used something as other than what it really was.

And yes, BCPL saw real use. I've used a desktop DOS that was written
in BCPL. The rest of the system was written in C, which made life
*very* interesting.

Now, as to why one would *want* languages that let you treat things as
other than what they were.

It's much easier to write functions that convert 16, 32 and 64 bit
quantities from network order to host order (and vice versa) if you
can treat them as an array of bytes, even though you'll want to treat
them as longer hunks while dealing with them. When talking to
hardware, you can get some really *strange* things. You may have a
location that is an address most of the time, but part of the time is
a control register full of bits to toggle. When doing cryptography,
you very often want to treat the string of characters you're
encrypting as a string of words of some length, because that's the
size chunk that the algorythm encrypts. Marshelling has already been
mentioned on this thread. You may well want to marshal ints and floats
in binary form, meaning you'll need to treat that array of bytes as
being of that type.

You can also look through the python library for places where struct
is used - most of those will involve doing something where you want to
treat a string of bytes as something else.

Finally, I don't see that there's that much difference between the two
different definitions of 'weakly typed'.  Both can be described as
treating an object as if it were of some type other than what it
really is. In one case, you abuse the raw bits, and in the other you
coerce the object to a different type. Both amount to the same thing:

       a = "10"
       b = 5
       c = a + b

In a strongly typed language, I get an error. In a weakly typed
language, I get something else. Either a pointer beyond the end of the
string a, or 15, depending on exactly how the a object is abused.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list