strong/weak typing and pointers

Steven Bethard steven.bethard at gmail.com
Thu Nov 4 22:43:22 CET 2004


Michael Hobbs <mike <at> hobbshouse.org> writes:
> 
> One word: union
> 

Interestingly, unions can be well-defined even in a strongly-typed language,
e.g. OCaml:

# type int_or_list = Int of int | List of int list;;
type int_or_list = Int of int | List of int list
# Int 1;;
- : int_or_list = Int 1
# List [1; 2];;
- : int_or_list = List [1; 2]

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!

Thanks,

Steve




More information about the Python-list mailing list