Ada Resource Association
News and resources for the Ada programming language
Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

7.5 Limited Types

1/2
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

2/2
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. 
2.1/2
  In the following contexts, an expression of a limited type is not permitted unless it is an aggregate, a function_call, or a parenthesized expression or qualified_expression whose operand is permitted by this rule: 
2.2/2
2.3/2
2.4/2
2.5/2
2.6/2
2.7/2
2.8/2
2.9/2

Static Semantics

3/2
A type is limited if it is one of the following: 
4/2
5/2
6/2
6.1/2
7
Otherwise, the type is nonlimited.
8
There are no predefined equality operators for a limited type. 

Implementation Requirements

8.1/2
  For an aggregate of a limited type used to initialize an object as allowed above, the implementation shall not create a separate anonymous object for the aggregate. For a function_call of a type with a part that is of a task, protected, or explicitly limited record type that is used to initialize an object as allowed above, the implementation shall not create a separate return object (see 6.5) for the function_call. The aggregate or function_call shall be constructed directly in the new object. 
NOTES
9/2
14  While it is allowed to write initializations of limited objects, such initializations never copy a limited object. The source of such an assignment operation must be an aggregate or function_call, and such aggregates and function_calls must be built directly in the target object. 
Paragraphs 10 through 15 were deleted. 
16
15  As illustrated in 7.3.1, an untagged limited type can become nonlimited under certain circumstances. 

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;
NOTES
21
16  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/2
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).

Contents   Index   References   Search   Previous   Next 
Ada-Europe Sponsored by Ada-Europe