pointersconstantsadasubtype

Access constant as subtype of general access type


For the null exclusion, it is possible to define a subtype of an access type that excludes the null values:

type    Day_Of_Month_Access          is access   Day_Of_Month;
subtype Day_Of_Month_Not_Null_Access is not null Day_Of_Month_Access;

I haven't found any reference for defining a subtype of an access type that removes the read-write possibility of the type, something like this (invented) example:

type    Day_Of_Month_Access          is access all Day_Of_Month;
subtype Day_Of_Month_Constant_Access is Day_Of_Month_Access with Access_Constant;

Is this kind of subtype impossible in current Ada? Why is it so?


Solution

  • I've spent a couple of days looking at this, and I believe the answer to be no, you cannot constrain a subtype using the constant keyword. The pertinent sections are:

    3.2.2 Subtype Declarations

    1    A subtype_declaration declares a subtype of some previously declared type, 
    as defined by a subtype_indication.
    
    Syntax
    2/3  subtype_declaration ::= subtype defining_identifier is subtype_indication [aspect_specification];
    3/2  subtype_indication ::=  [null_exclusion] subtype_mark [constraint]
    4    subtype_mark ::= subtype_name
    5    constraint ::= scalar_constraint | composite_constraint
    6    scalar_constraint ::= range_constraint | digits_constraint | delta_constraint
    7    composite_constraint ::= index_constraint | discriminant_constraint
    

    Note here the lack of mentioning "constant" in the subtype definition

    and: 3.10 Access Types

    1    A value of an access type (an access value) provides indirect access to the 
    object or subprogram it designates. Depending on its type, an access value can 
    designate either subprograms, objects created by allocators (see 4.8), or more 
    generally aliased objects of an appropriate type.
    
    Syntax
    2/2    access_type_definition ::= [null_exclusion] access_to_object_definition 
      | [null_exclusion] access_to_subprogram_definition
    3      access_to_object_definition ::= access [general_access_modifier] subtype_indication
    4      general_access_modifier ::= all | constant
    5      access_to_subprogram_definition ::= access [protected] procedure parameter_profile 
      | access [protected] function  parameter_and_result_profile
    5.1/2  null_exclusion ::= not null
    6/2    access_definition ::= [null_exclusion] access [constant] subtype_mark
      | [null_exclusion] access [protected] procedure parameter_profile
      | [null_exclusion] access [protected] function parameter_and_result_profile
    

    You can see here that null_exclusion and constant are in separate parts of the definition, so they seem unrelated from each other.