Go to the first, previous, next, last section, table of contents.


3.5.5 Operations of Discrete Types

Static Semantics

  1. For every discrete subtype S, the following attributes are defined:
  2. S'Pos
      S'Pos denotes a function with the following specification:
    1. function S'Pos(Arg : S'Base)
        return universal_integer
      
    2. This function returns the position number of the value of Arg, as a value of type universal_integer.

  1. S'Val
      S'Val denotes a function with the following specification:
    1. function S'Val(Arg : universal_integer)
        return S'Base
      
    2. This function returns a value of the type of S whose position number equals the value of Arg. For the evaluation of a call on S'Val, if there is no value in the base range of its type with the given position number, Constraint_Error is raised.

Implementation Advice

  1. For the evaluation of a call on S'Pos for an enumeration subtype, if the value of the operand does not correspond to the internal code for any enumeration literal of its type (perhaps due to an uninitialized variable), then the implementation should raise Program_Error. This is particularly important for enumeration types with noncontiguous internal codes specified by an enumeration_representation_clause.

    NOTES

  2. (28) Indexing and loop iteration use values of discrete types.
  3. (29) The predefined operations of a discrete type include the assignment operation, qualification, the membership tests, and the relational operators; for a boolean type they include the short-circuit control forms and the logical operators; for an integer type they include type conversion to and from other numeric types, as well as the binary and unary adding operators - and +, the multiplying operators, the unary operator abs, and the exponentiation operator. The assignment operation is described in See section 5.2 Assignment Statements. The other predefined operations are described in Section See section 4 Names and Expressions.
  4. (30) As for all types, objects of a discrete type have Size and Address attributes, See section 13.3 Representation Attributes.
  5. (31) For a subtype of a discrete type, the result delivered by the attribute Val might not belong to the subtype; similarly, the actual parameter of the attribute Pos need not belong to the subtype. The following relations are satisfied (in the absence of an exception) by these attributes:
  6. S'Val(S'Pos(X)) = X
    S'Pos(S'Val(N)) = N
    

    Examples

  7. Examples of attributes of discrete subtypes:
  8. --  For the types and subtypes declared in subclause
    --  See section 3.5.1 Enumeration Types, the following hold:
    
  9. --  Color'First   = White,   Color'Last   = Black
    --  Rainbow'First = Red,     Rainbow'Last = Blue
    
  10. --  Color'Succ(Blue) = Rainbow'Succ(Blue) = Brown
    --  Color'Pos(Blue)  = Rainbow'Pos(Blue)  = 4
    --  Color'Val(0)     = Rainbow'Val(0)     = White
    


Go to the first, previous, next, last section, table of contents.