Contents Index Previous Next
7.5 Limited Types
1
[
{Limited type} [Glossary
Entry]
A limited type is (a view of) a type for which
the assignment operation is not allowed. A nonlimited type is a (view
of a) type for which the assignment operation is allowed.]
1.a
Discussion: The concept
of the value of a limited type is difficult to define, since the
abstract value of a limited type often extends beyond its physical representation.
In some sense, values of a limited type cannot be divorced from their
object. The value is the object.
1.b
In Ada 83, in the two places where
limited types were defined by the language, namely tasks and files, an
implicit level of indirection was implied by the semantics to avoid the
separation of the value from an associated object. In Ada 95, most limited
types are passed by reference, and even return-ed by reference.
1.c
To be honest: For a limited
partial view whose full view is nonlimited, assignment is possible on
parameter passing and function return. To prevent any copying whatsoever,
one should make both the partial and full views limited.
Legality Rules
2
If a tagged record type has any limited components,
then the reserved word limited shall appear in its record_type_definition.
2.a
Reason:
This prevents tagged limited types from becoming nonlimited. Otherwise,
the following could happen:
2.b
package P is
type T is limited private;
type R is tagged
record -- Illegal!
-- This should say ``limited record''.
X : T;
end record;
private
type T is new Integer; -- R becomes nonlimited here.
end P;
2.c
package Q is
type R2(Access_Discrim : access ...) is new R with
record
Y : Some_Task_Type;
end record;
end Q;
2.d
If the above were legal, then
assignment would be defined for R'Class in the body of P, which is bad
news, given the access discriminant and the task.
Static Semantics
3
{limited
type} A type is
limited if it is
a descendant of one of the following:
4
- a type with the reserved word limited in its definition;
4.a
Ramification: Note that
there is always a ``definition,'' conceptually, even if there is no syntactic
category called ``..._definition''.
5
- a task or protected type;
6
- a composite type with a limited component.
7
{nonlimited type}
Otherwise, the type is nonlimited.
8
[There are no predefined equality operators for
a limited type.]
9
13 The
following are consequences of the rules for limited types:
10
- An initialization expression is not allowed in an object_declaration
if the type of the object is limited.
11
- A default expression is not allowed in a component_declaration
if the type of the record component is limited.
12
- An initialized allocator is not allowed if the designated
type is limited.
13
- A generic formal parameter of mode in must not be
of a limited type.
14
14 Aggregates
are not available for a limited composite type. Concatenation is not
available for a limited array type.
15
15 The rules do not exclude
a default_expression for a formal
parameter of a limited type; they do not exclude a deferred constant
of a limited type if the full declaration of the constant is of a nonlimited
type.
16
16 {become nonlimited}
{nonlimited type (becoming nonlimited)}
{limited type (becoming nonlimited)}
As illustrated in 7.3.1, an
untagged limited type can become nonlimited under certain circumstances.
16.a
Ramification: Limited private
types do not become nonlimited; instead, their full view can be nonlimited,
which has a similar effect.
16.b
It is important to remember that
a single nonprivate type can be both limited and nonlimited in different
parts of its scope. In other words, ``limited'' is a property that depends
on where you are in the scope of the type. We don't call this a ``view
property'' because there is no particular declaration to declare the
nonlimited view.
16.c
Tagged types never become nonlimited.
Examples
17
Example of a
package with a limited type:
18
package IO_Package is
type File_Name is limited private;
19
procedure Open (F : in out File_Name);
procedure Close(F : in out File_Name);
procedure Read (F : in File_Name; Item : out Integer);
procedure Write(F : in File_Name; Item : in Integer);
private
type File_Name is
limited record
Internal_Name : Integer := 0;
end record;
end IO_Package;
20
package body IO_Package is
Limit : constant := 200;
type File_Descriptor is record ... end record;
Directory : array (1 .. Limit) of File_Descriptor;
...
procedure Open (F : in out File_Name) is ... end;
procedure Close(F : in out File_Name) is ... end;
procedure Read (F : in File_Name; Item : out Integer) is ... end;
procedure Write(F : in File_Name; Item : in Integer) is ... end;
begin
...
end IO_Package;
21
17 Notes on the example:
In the example above, an outside subprogram making use of IO_Package
may obtain a file name by calling Open and later use it in calls to Read
and Write. Thus, outside the package, a file name obtained from Open
acts as a kind of password; its internal properties (such as containing
a numeric value) are not known and no other operations (such as addition
or comparison of internal names) can be performed on a file name. Most
importantly, clients of the package cannot make copies of objects of
type File_Name.
22
This example is characteristic of any
case where complete control over the operations of a type is desired.
Such packages serve a dual purpose. They prevent a user from making use
of the internal structure of the type. They also implement the notion
of an encapsulated data type where the only operations on the type are
those given in the package specification.
23
The fact that the full view of File_Name is explicitly
declared limited means that parameter passing and function return will
always be by reference (see 6.2 and 6.5).
Extensions to Ada 83
23.a
{extensions to Ada 83}
The restrictions in RM83-7.4.4(4), which disallowed
out parameters of limited types in certain cases, are removed.
Wording Changes from Ada 83
23.b
Since limitedness and privateness are orthogonal
in Ada 95 (and to some extent in Ada 83), this is now its own clause rather
than being a subclause of 7.3, ``Private
Types and Private Extensions''.
Contents Index Previous Next Legal