[9]
DejaVU Online:
Principles of Object-Oriented Software Development
(©)
, is an
extension of
with type expressions of the form
(to denote existential types)
and expressions of the form
(to denote values with hidden types).
Intuitively, the meaning of the expression
is that we represent the abstract type
occurring
in the type expression
by the actual type
(in order to realize the value e).
Following the type assignment rule, we may actually provide
an instance of a subtype of the bounding type
as the realization of a hidden type.
See slide 9-existential.
to denote
.)
First, existential types allow us to
indicate that the realization of a particular type exists,
even if we do not indicate how.
The declaration
tells us that
there must be some type
such that e of type
can be realized.
Apart from claiming that a particular
type exists, we may also provide information
concerning its structure, while leaving its actual type
undetermined.
(which may clearly be realized
by any type) carries no information whatsoever, hence it may
be considered to be equal
to the type Top.
More information, for example, is provided by the type
which defines the product type
consisting of two (possibly distinct) types.
(A product may be regarded as an unlabeled record.)
The type
gives even more information
concerning the structure of a product type,
namely that the two components are of the same type.
Hence, for the actual product
the latter is the best choice.
See slide 9-ex-existential.
Existential types may be used to impose structure on the
contents of a value,
while hiding its actual representation.
For example, when we have a variable x
of which we know that it has type
then we may use the second component of x
to produce an integer value from its first component,
by
, where fst extracts the first
and snd the second component of a product.
Clearly, we do not need to know the actual
representation type for
.
A similar idea may be employed for (labeled) records.
For example, when we have a record x of type
then we may
use the expression
to apply the
operation op to the value val.
Again, no knowledge of the type of val is required in this case.
However, to be able to use an element of an existential type
we must provide an actual representation type,
by instantiating the type parameter in a pack
statement.
calculus
that supports recursive (object) types and inheritance.
class event { event
protected:
event(event* x) : ev(x) {}
public:
int type() { return ev->type(); }
void* rawevent() { return ev; }
private:
event* ev;
};
class xevent : public event { X
public:
int type() { return X->type(); }
private:
struct XEvent* X;
};
|
Hush Online Technology
hush@cs.vu.nl
12/29/99 |
|
|