strong/weak typing and pointers

Michael Hobbs mike at hobbshouse.org
Fri Nov 5 00:03:29 CET 2004


Steven Bethard <steven.bethard at gmail.com> wrote:
> The reason for this is that at any given time in OCaml, the sequence of bits is
> only interpretable as *one* of the two types, never both.  If you have a good
> example of using a union (in C probably, since OCaml wouldn't let you do this I
> don't think) where you want to treat a given sequence of bytes as both types *at
> once*, that would be great!

This example is a little weak, but may be sufficient. The in_addr
structure used for sockets usually uses a union to provide different
views to the underlying 32-bit address. You can access the address
as 4 8-bit values, 2 16-bit values, or 1 32-bit value. Most code
these days only use the 4 8-bit representation, but the interface is
there.

Another possible example comes from the Windows API. Some of the
functions take an arbitrary length structure. If you want to make a
simple call to the function, you pass a small structure. If you 
want to make a more complex call to the function, you pass a larger
structure that has more fields tacked on to the end. Usually, the 
first field in the structure is an int that specifies how large the
structure is. It is used as sort of a crude version of OO in C.

I'm not sure if these are the kinds of examples you're looking for.
I don't know how anyone would be able to use a sequence of bytes as
two types of data at once. There is almost always some sort of 
indicator that specifies how to interpret the bytes; otherwise, it
is just garbage.

-- Mike



More information about the Python-list mailing list