Simple types

We touched upon some simple *types* in Section
where we saw examples of variables of type `real`, ` integer`, `posint`, `boolean` and `string`.
*Types* have the following pragmatic advantages:

- they make your programs easier to understand,
- they reduce the number of errors through a mechanism called
*type checking*, and - they allow one to organize our data in an intuitive natural manner.

All data has a type associated with it and
to test the type of an item of data there is a built-in Darwin function

`type`(*exp, tn*)

where *exp* is any expression and *tn* is any Darwin type.
This function returns `true` if the expression *exp*evaluates to type *tn* or `false` otherwise.

For instance,

> type(5, real); # real numbers are integers and true > type(1.3, real); # numbers with fractional parts true > type(5, integer); # integers positive whole numbers true > type(-5, integer); # and negative whole numbers true > type(5.5, integer); # no fractions allowed in type integer false > type(99, posint); # positive integers are integers # greater than zero true > type(0, posint); # 0 is not a positive integer false > type('hello', string); # strings surrounded with single quotes true > type(true, boolean); # true and false are booleans true > type(5.0, integer); # maybe this one is a bit of a surprise true

In fact, even variable names have a type associated with them.
Any legal variable name which has not been assigned a value in
Darwin has type `name`.

> type(this_is_the_longest_variable_name_in_the_world, name); > type(R2D2, name); > type(_toying_with_disaster, name);(see § for the criteria of what constitutes a legal name).

The type `boolean` has two values: `true` and `false`.
The `real` and `integer` types correspond to the mathematical
concepts of a real and integer numbers respectively. The `posint`
type is an abbreviation of *positive integer*. The `posint`
type does not include the number zero. The relationships between
these three types is shown in Figure .

An object of type `string` is a sequence of symbols surrounded by the
single quote symbols (`'`

).

From these simple types, we can build the more complex *structured
types* that we will encounter in later chapters of this book.