Version 1.5 of ais/ai-00313.txt

Unformatted version of ais/ai-00313.txt version 1.5
Other versions for file ais/ai-00313.txt

!standard 8.3 (16)          02-09-27 AI95-00313/01
!standard 8.3 (17)
!class amendment 02-09-27
!status No Action (8-0-2) 02-10-11
!status work item 02-09-27
!status received 02-09-20
!priority Low
!difficulty Easy
!subject Self-reference in subprogram access
!summary
The name of an access-to-subprogram type can be used in the declaration of the type.
!problem
It is very inconvinient to write an access-to-subprogram type which takes a parameter of the type itself.
!proposal
Add after 8.3(17):
For an access-to-subprogram type, the declaration is hidden from all visibility only until the reserved keyword access;
!discussion
For example:
type Chaining_Procedure is access procedure (...<other parameters>...; Next_Proc : Chaining_Procedure);
This is illegal by 8.3(16). Declaring Chaining_Procedure as an incomplete type prior to this doesn't help, because of 8.3(19).
The best workaround seems to be to add a new type:
type Chaining_Procedure_2; type Chaining_Procedure is access procedure
(...<other parameters>...; Next_Proc : Chaining_Procedure_2);
type Chaining_Procedure_2 is new Chaining_Procedure;
which requires the use of type conversions between the types.
!example
!ACATS test
An ACATS C-Test should be created.
!appendix

!topic Self-reference in subprogram access
!reference RM95 8.3(16-18), 3.10
!from Adam Beneschan 09-20-02
!discussion

Is there any reason why this should be illegal?

   type Chaining_Procedure is access procedure
            (...<other parameters>...; Next_Proc : Chaining_Procedure);

This is illegal by 8.3(16) [and declaring Chaining_Procedure as an
incomplete type prior to this doesn't help, because of 8.3(19)].
However, someone may want to do something like this, and there aren't
any implementation problems that I can think of.

(The best workaround I can think of is to add a new type:

   type Chaining_Procedure_2;
   type Chaining_Procedure is access procedure
            (...<other parameters>...; Next_Proc : Chaining_Procedure_2);
   type Chaining_Procedure_2 is new Chaining_Procedure;

which may necessitate a type conversion in some cases.)

Anyway, this may not be a high priority, but it seems harmless to me
to add another exception after 8.3(17) that for an
access-to-subprogram type declaration, the declaration is hidden from
all visibility only until the reserved word "access".

****************************************************************

From: Randy Brukardt
Sent: Friday, September 27, 2002  3:10 PM

I do agree its harmless, but even so, its additional work. (Figuring out how
to change 'hidden from all visibility' is non-trivial in Janus/Ada, at
least). Given that, I'd like to know if anyone has run into this problem in
practice. If not, perhaps it is not worth the effort to change even if it
would be relatively easy.

****************************************************************

From: Adam Beneschan
Sent: Wednesday, November  5, 2003  11:41 AM

Back in September 2002, we had discussed the possibility of allowing
the declaration of an access-to-subprogram type T to refer to T in the
subprogram parameters or return type:

[Adam Beneschan:]
>
> Is there any reason why this should be illegal?
>
>    type Chaining_Procedure is access procedure
>             (...<other parameters>...; Next_Proc :
>             Chaining_Procedure);
>
> This is illegal by 8.3(16) [and declaring Chaining_Procedure as an
> incomplete type prior to this doesn't help, because of 8.3(19)].
> However, someone may want to do something like this, and there aren't
> any implementation problems that I can think of.
>
> (The best workaround I can think of is to add a new type:
>
>    type Chaining_Procedure_2;
>    type Chaining_Procedure is access procedure
>             (...<other parameters>...; Next_Proc :
>    Chaining_Procedure_2);
>    type Chaining_Procedure_2 is new Chaining_Procedure;
>
> which may necessitate a type conversion in some cases.)
>
> Anyway, this may not be a high priority, but it seems harmless to me
> to add another exception after 8.3(17) that for an
> access-to-subprogram type declaration, the declaration is hidden from
> all visibility only until the reserved word "access".

[Randy Brukardt:]
>
> I do agree its harmless, but even so, its additional work. (Figuring
> out how to change 'hidden from all visibility' is non-trivial in
> Janus/Ada, at least). Given that, I'd like to know if anyone has run
> into this problem in practice. If not, perhaps it is not worth the
> effort to change even if it would be relatively easy.

It appears that someone has now run into this problem; see the thread
"Problem with Self-Referential Access-To-Subprogram Type" on
comp.lang.ada.  Here's the source the poster (a self-described "Ada
newbie") was trying to use:

    package Test is
       --  A little ADT:
       type Test_Type is tagged private;
       function Get_Dummy (Tester : Test_Type) return Integer;
       procedure Set_Dummy (Tester : in out Test_Type; Dummy : in Integer);

       --  An access-to-function type that works (but Tester is read-only):
       type State1;
       type State1 is access function (Tester : Test_Type'Class)
                                      return State1;
       --  An access-to-procedure type that doesn't work:
       type State2;
       type State2 is access procedure (Tester : in out Test_Type'Class;
                                        Resultant_State : out State2);
       --                premature use of "State2" right here ^
    private
       type Test_Type is
         tagged record
            Dummy : Integer;
         end record;
    end Test;

(The declaration of State1 should not have compiled.)

****************************************************************

Questions? Ask the ACAA Technical Agent