> type?<<. [...]
The word type is used to refer to two related but distinct
ideas in computer programming. Historically these ideas
were not distinguished (in Fortran, for example), so a lot
of people still confuse the two meanings. It's important
to understand the difference because, one, what makes sense
for one meaning sometimes doesn't for the other, and two,
the essence of OOP lies in realizing the distinction and
maximizing the gulf between the two different kinds of
"type". Let me try to illustrate.
The earliest understanding of "type" has to do with layout,
structure, or representation. If we have a variable of type
'int', the bits that make up the value will be positioned in
a particular way, so that what is stored in memory can be
interpreted as an abstract numeric value. I will refer to
this meaning of "type" as /structure/.
The other meaning of "type" has to do with what is expected,
or allowed, for a programmatic entity (eg, a variable or an
expression), when the program is being compiled. In the early
days of computing the only kind of thing that was expected is
what structure the variable or expression would have at runtime,
which explains why people used to think of the two ideas as the
same thing.
Let's look at a couple of examples. Consider these two variable
declarations:
static int i = 4;
static const int ci = 4;
The structure of these two variables is exactly the same: if we
looked at the bits, we would find the same pattern of zeros and
ones in both variables.
But what is expected (or allowed) for the two variables is /not/
the same. The variable 'i' is changeable, eg, with an assignment
statement. The variable 'ci' may not be changed via assignment.
Furthermore, even though it may be possible to change 'ci' by means
of some language or extra-linguistic trickery (eg, assembly), the
compiler may assume its value will /not/ change. In this case the
"type" of a variable is more specific than its structure.
For another example, consider this printf() call:
printf( "%.8x", 127 );
A %x conversion specifier expects an argument of type unsigned
int, but we have given for the actual argument a value of type
int. The printf() call still works, because 'int' and 'unsigned
int' are guaranteed to have the same representation for values in
the non-negative range of int. In some sense the "type" expected
is less specific than the structure information of the actual
argument value.
In C++ a good example illustrating the distinction between the
two aspects is a parameter of type Base*, where in a particular
call the argument value supplied is of type Derived*. The
structure of the object pointed to at runtime may not be the same
as what the compile-time pointer type nominally implies.
A note on terminology: the two kinds of "type" are sometimes
referred to as "dynamic type" or "run-time type", for the
layout/structure/representation aspect, and "compile-time type"
or often just plain "type", for the what-is-expected-or-allowed
aspect. I prefer the terms "structure" and "type", respectively,
for these two distinct aspects, but the important thing is to
understand the distinction (assuming of course that what terms
one uses for the two different aspects is made clear).
The essential idea in OOP is to decouple these two aspects as
much as possible. In Smalltalk, every value is an object
pointer, and that's it. What can we do with an object pointer?
Pass it as a value, store it in a variable, or send it a message
(and importantly, any message). All of those are true no matter
what class the object being pointed to belongs. In other words
the type -- ie, what is expected or allowed in terms of language
usage -- is completely decoupled from the class, or structure,
of what the object is at runtime. Obviously some of those things
will be nonsensical (eg, will result in "Message not understood"
popups), but the principle involved is at the heart of OOP and
OOD. (Historical note: in its early usage "object-oriented
programming" was understood to include what is now called design
or "object-oriented design", OOD/A, etc.)
A more complete explanation of the difference between "type"
and "structure" may be found in this paper:
A Type Declaration and Inference System for Smalltalk
http://dl.acm.org/citation.cfm?id=582168
Does my explanation help illuminate what "type" means in its
several aspects?