Chapter 8
Collections
Element Types
Each instance X of <collection>
has a conceptual element type that is an instance
of <type>
. If the element type of X
is T, X stores elements of
type T. The element
method will always return an
instance of T and the element-setter
method
(if X is mutable) will accept any instance of T. The analogous
functions returned by the iteration protocol also return/accept
any instance of T.
Each subclass C of <collection>
has a conceptual element type that is either T1 or
indefinite ⇐
T1,
where T1 is a type. (The symbol
in
the ⇐
indefinite
notation is an
abbreviation for ⇐
T1subtype.
)
If the element type of C is T1, each general instance of C must have an element type T2 that is type equivalent to T1. Each subclass of C must have an element type T3 that is type equivalent to T1.
If the element type of C is
indefinite ⇐
T1, each general instance
of C must have an element type T2 that is a
subtype of T1. Therefore, element
called on that
instance will return an instance of T1 (and will not return
all possible instances of T1
if T2 is a proper subtype
of T1). It is not determined by C what the
applicable element-setter
method will accept (thus C's element type
is said to be indefinite
). Each subclass of C must have element
type T3 or
indefinite ⇐
T3,
where T3 is a subtype
of T1.
User-defined collection classes must also follow these rules.
Note: the above statements about the value returned by element
only apply when
no default:
keyword argument is specified.
Collection |
Element Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: K1 and K2 are subtypes
of <character>
that have not been given standardized
names.
By convention, if C is an instantiable subtype
of <collection>
and C's element-type is
indefinite ⇐
<object>
, then instantiating C
produces a collection whose element type is <object>
.
Instantiating <range>
produces a collection whose element-type is
unspecified except that it is a subtype of <real>
and every element of
the range is an instance of the element type.
The preceding section describes the element type of every object that is created
by make
of an instantiable built-in collection
class. The element type of an instance of a user-defined
collection class is unspecified, but should follow the rules given here in order to preserve
the property that any operation that works on an instance of a supertype must work on an
instance of a subtype.