Prev | Up | Next | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help


3.9 Tagged Types and Type Extensions -- TOC

(1)
Tagged types and type extensions support object-oriented programming, based on inheritance with extension and run-time polymorphism via dispatching operations.
Static Semantics
(2)
A record type or private type that has the reserved word tagged in its declaration is called a tagged type. When deriving from a tagged type, additional components may be defined. As for any derived type, additional primitive subprograms may be defined, and inherited primitive subprograms may be overridden. The derived type is called an extension of the ancestor type, or simply a type extension. Every type extension is also a tagged type, and is either a record extension or a private extension of some other tagged type. A record extension is defined by a derived_type_definition with a record_extension_part. A private extension, which is a partial view of a record extension, can be declared in the visible part of a package (see 7.3) or in a generic formal part (see 12.5.1).
(3)
An object of a tagged type has an associated (run-time) tag that identifies the specific tagged type used to create the object originally. The tag of an operand of a class-wide tagged type T'Class controls which subprogram body is to be executed when a primitive subprogram of type T is applied to the operand (see 3.9.2); using a tag to control which body to execute is called dispatching.
(4)
The tag of a specific tagged type identifies the full_type_declaration of the type. If a declaration for a tagged type occurs within a generic_package_declaration, then the corresponding type declarations in distinct instances of the generic package are associated with distinct tags. For a tagged type that is local to a generic package body, the language does not specify whether repeated instantiations of the generic body result in distinct tags.
(5)
The following language-defined library package exists:
(6)
       package Ada.Tags is
           type Tag is private;
(7)
           function Expanded_Name(T : Tag) return String;
           function External_Tag(T : Tag) return String;
           function Internal_Tag(External : String) return Tag;
(8)
           Tag_Error : exception;
(9)
       private
          ... -- not specified by the language
       end Ada.Tags;
(10)
The function Expanded_Name returns the full expanded name of the first subtype of the specific type identified by the tag, in upper case, starting with a root library unit. The result is implementation defined if the type is declared within an unnamed block_statement.
(11)
The function External_Tag returns a string to be used in an external representation for the given tag. The call External_Tag(S'Tag) is equivalent to the attribute_reference S'External_Tag (see 13.3).
(12)
The function Internal_Tag returns the tag that corresponds to the given external tag, or raises Tag_Error if the given string is not the external tag for any specific type of the partition.
(13)
For every subtype S of a tagged type T (specific or class-wide), the following attributes are defined:
(14)
S'Class
(15)
(16)
S'Tag
(17)
Given a prefix X that is of a class-wide tagged type (after any implicit dereference), the following attribute is defined:
(18)
X'Tag
Dynamic Semantics
(19)
The tag associated with an object of a tagged type is determined as follows:
(20)
(21)
(22)
(23)
(24)
(25)
The tag is preserved by type conversion and by parameter passing. The tag of a value is the tag of the associated object (see 6.2).
Implementation Permissions
(26)
The implementation of the functions in Ada.Tags may raise Tag_Error if no specific type corresponding to the tag passed as a parameter exists in the partition at the time the function is called.

(27)
(28)
(29)
(30)
Examples
(31)
Examples of tagged record types:
(32)
       type Point is tagged
         record
           X, Y : Real := 0.0;
         end record;
(33)
       type Expression is tagged null record;
         -- Components will be added by each extension

Subclauses

  1. Type Extensions
  2. Dispatching Operations of Tagged Types
  3. Abstract Types and Subprograms


Prev | Up | Next | Back | Forward
TOC -- / --.-- / --.--.-- | Index | Search | Syntax | Help

Ada WWW Home -- Email comments, additions, corrections, gripes, kudos, etc. to:

Magnus Kempe -- Magnus.Kempe@di.epfl.ch
Copyright statement
Page last generated: 95-03-12