7.5 Limited Types
A limited type is (a view of) a type for which copying
(such as for an
assignment_statement)
is not allowed. A nonlimited type is a (view of a) type for which copying
is allowed.
Legality Rules
If a tagged record type has any limited components,
then the reserved word
limited shall appear in its
record_type_definition.
If the reserved word
limited appears in the definition of a
derived_type_definition,
its parent type and any progenitor interfaces shall be limited.
In the following contexts,
an
expression
of a limited type is permitted only if each of its operative constituents
is newly constructed (see
4.4):
the return expression of an expression function
(see
6.8)
Static Semantics
A
view of a type is
limited if it is one of the following:
a type with the reserved word limited, synchronized,
task, or protected in its definition;
a class-wide type whose specific type is limited;
a composite type with a limited component;
an incomplete view;
a derived type whose parent is limited and is not
an interface.
Otherwise, the type is nonlimited.
There are no predefined equality operators for a
limited type.
A type is
immutably
limited if it is one of the following:
An explicitly limited record type;
A record extension with the reserved word limited;
A nonformal limited private type that is tagged
or has at least one access discriminant with a
default_expression;
A task type, a protected type, or a synchronized
interface;
A type derived from an immutably limited type.
A descendant of a generic formal limited private
type is presumed to be immutably limited except within the body of a
generic unit or a body declared within the declarative region of a generic
unit, if the formal type is declared within the formal part of the generic
unit.
NOTE 1 While it is allowed to write
initializations of limited objects, such initializations never copy a
limited object. The source of such an assignment operation will be an
aggregate
or
function_call,
and such
aggregates
and
function_calls
will be built directly in the target object (see
7.6).
Paragraphs 10 through
15 were deleted.
NOTE 2
As
illustrated in
7.3.1, an untagged limited
type can become nonlimited under certain circumstances.
Examples
Example of a package
with a limited type:
package IO_Package is
type File_Name is limited private;
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;
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;
In the example, an outside subprogram making use
of IO_Package can 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.
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.
The fact that the full view of File_Name is explicitly
declared
limited means that parameter passing will always be by
reference and function results will always be built directly in the result
object (see
6.2 and
6.5).
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe