comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: Q: access to subprogram
Date: 1996/07/03
Date: 1996-07-03T00:00:00+00:00	[thread overview]
Message-ID: <4re8bj$qfk@krusty.irvine.com> (raw)
In-Reply-To: DtytvM.Mw2@world.std.com


It should be noted that the new object-oriented features of Ada 95
should make subprogram accesses less necessary than before.  Abstract
types inherently possess subprogram pointers, so it should be possible
to take advantage of this feature as an alternative to explicit
subprogram accesses.

In Ada 83, I missed being able to do things like this:

    with Pack1;
    procedure P1 is
        Error_File : Text_IO.File_Type;

        procedure Report_Error (Message : string) is
        begin
            Text_IO.Put_Line (Error_File, Message);
        end Report_Error;

    begin
        Text_IO.Create (Error_File, ...
        ...
        Pack1.Do_Some_Big_Procedure_That_Can_Report_Errors (Report_Error);
            -- some people would fire me for using a name like this  :)
        ...
    end P1;

The purpose is to call a procedure that can generate error messages,
and to tell it what I want done with those error messages.  Things
can't be done this way in Ada 83.  The problem can be solved using
generics in Ada 83, but I've always thought of this as a strange
reason for using generics, and it can get hairy (suppose, for example,
that the big procedure calls another procedure that also reports
errors; now that procedure has to be turned into a generic also, and
so on).

In Ada 95, you can accomplish this with a subprogram access, but you
have to make Report_Error global, and therefore Error_File has to be
made global also.  So doing things this way forces you to give up some
of the design advantages of using local variables.  (It isn't too big
a problem in the above example, but in more complicated situations,
making things global can be a real pain.)  This is also one of the
things I missed when I was programming in C, since C doesn't have
nested procedures.

Another way to do this is with an abstract type.  (PLEASE NOTE: I
don't guarantee that my Ada 95 syntax is correct!)  You can declare an
"error handler" in some global package like this:

    type Error_Handler is abstract tagged null record;
    procedure Handle_Error (Handler : in Error_Handler;
                            Message : in string) is abstract;

The Do_Some_Big_Procedure_That_Can_Report_Errors can then take an
Error_Handler (or Error_Handler'Class?) as a parameter.  It calls the
Handle_Error routine when it wants to generate an error message.  It
can pass the Error_Handler object around to other procedures that also
report errors.  P1 sets things up to use its own error reporting
routine as the error handler:

    procedure P1 is
        
        type My_Error_Handler is new Error_Handler with record
            Error_File : Text_IO.File_Type;
        end record;
        
        EH : My_Error_Handler;
                -- variable name chosen to avoid being fired  :)
        
        procedure Handle_Error (Handler : in My_Error_Handler;
                                Message : in string) is 
        begin
            Text_IO.Put_Line (Handler.Error_File, Message);
        end Handle_Error;

    begin
        Text_IO.Create (EH.Error_File, ...
        ...
        Pack1.Do_Some_Big_Procedure_That_Can_Report_Errors (EH);

This mechanism doesn't let you *directly* store an access to a
subprogram in a global variable, the way subprogram accesses do; but
using accesses to Error_Handler objects should let you accomplish the
same thing.

Once again, my apologies if my Ada 95 syntax is incorrect.  I haven't
yet learned all the nuances of Ada 95 programming.  Mainly, I wanted
to discuss the concepts involved; someone else may need to fill in the
programming details.

                                -- Adam

            




  reply	other threads:[~1996-07-03  0:00 UTC|newest]

Thread overview: 133+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-02  0:00 Q: access to subprogram tmoran
1996-07-02  0:00 ` Robert A Duff
1996-07-02  0:00   ` Robert Dewar
1996-07-03  0:00   ` Jon S Anthony
1996-07-03  0:00     ` Robert A Duff
1996-07-08  0:00       ` Norman H. Cohen
1996-07-09  0:00         ` Robert A Duff
1996-07-03  0:00     ` Robert Dewar
1996-07-03  0:00     ` Mark A Biggar
1996-07-03  0:00       ` Robert A Duff
1996-07-03  0:00         ` Robert Dewar
1996-07-09  0:00         ` Thomas Wolff
1996-07-09  0:00           ` Robert Dewar
1996-07-10  0:00           ` Robert A Duff
1996-07-10  0:00             ` Richard A. O'Keefe
1996-07-10  0:00               ` Robert A Duff
1996-07-10  0:00                 ` Thomas Wolff
1996-07-10  0:00                   ` Robert A Duff
1996-07-10  0:00                   ` Robert Dewar
1996-07-10  0:00               ` Robert Dewar
1996-07-03  0:00       ` Robert Dewar
1996-07-06  0:00         ` Robert A Duff
1996-07-08  0:00           ` Norman H. Cohen
1996-07-08  0:00             ` Robert Dewar
1996-07-11  0:00             ` Robert A Duff
1996-07-12  0:00               ` Robert A Duff
1996-07-14  0:00               ` Norman H. Cohen
1996-07-19  0:00     ` Brian Rogoff
1996-07-22  0:00       ` Richard A. O'Keefe
1996-07-23  0:00       ` Brian Rogoff
1996-07-23  0:00         ` Robert A Duff
1996-07-26  0:00         ` Brian Rogoff
1996-07-28  0:00           ` Robert A Duff
1996-07-22  0:00     ` Brian Rogoff
1996-07-23  0:00       ` Robert A Duff
1996-07-24  0:00       ` Brian Rogoff
1996-07-26  0:00         ` Robert A Duff
1996-07-30  0:00         ` Brian Rogoff
1996-07-24  0:00       ` Richard A. O'Keefe
1996-07-26  0:00         ` Ken Garlington
1996-07-30  0:00           ` Richard A. O'Keefe
1996-07-24  0:00     ` Brian Rogoff
1996-07-26  0:00     ` Richard A. O'Keefe
1996-07-28  0:00       ` Fergus Henderson
1996-07-28  0:00       ` Robert A Duff
1996-07-29  0:00         ` Richard A. O'Keefe
1996-07-29  0:00           ` Robert A Duff
1996-07-29  0:00     ` Richard A. O'Keefe
1996-07-30  0:00     ` Jon S Anthony
1996-07-03  0:00   ` Fergus Henderson
1996-07-03  0:00     ` Robert A Duff
1996-07-03  0:00       ` Adam Beneschan [this message]
1996-07-03  0:00         ` Robert Dewar
1996-07-03  0:00         ` Robert A Duff
1996-07-09  0:00         ` Thomas Wolff
1996-07-03  0:00       ` Robert Dewar
1996-07-05  0:00   ` Jon S Anthony
1996-07-06  0:00     ` Robert Dewar
1996-07-06  0:00     ` Robert A Duff
1996-07-06  0:00       ` Robert Dewar
1996-07-08  0:00         ` Robert A Duff
1996-07-08  0:00       ` Richard A. O'Keefe
1996-07-08  0:00         ` Robert A Duff
1996-07-08  0:00           ` Robert Dewar
1996-07-08  0:00         ` Robert Dewar
1996-07-10  0:00           ` Richard A. O'Keefe
1996-07-10  0:00             ` Robert Dewar
1996-07-19  0:00               ` Richard A. O'Keefe
1996-07-07  0:00   ` Ronald Cole
1996-07-07  0:00     ` Robert Dewar
1996-07-07  0:00       ` Richard Kenner
1996-07-07  0:00         ` Robert Dewar
1996-07-14  0:00       ` Ronald Cole
1996-07-14  0:00         ` Richard Kenner
1996-07-15  0:00           ` Fergus Henderson
1996-07-15  0:00             ` Robert Dewar
1996-07-17  0:00               ` Fergus Henderson
1996-07-17  0:00                 ` Richard Kenner
1996-07-17  0:00               ` Adam Beneschan
1996-07-20  0:00               ` Michael Feldman
1996-07-20  0:00                 ` Robert Dewar
1996-07-16  0:00             ` Richard Kenner
1996-07-07  0:00     ` Richard Kenner
1996-07-07  0:00   ` Mark Eichin
1996-07-08  0:00     ` Richard Kenner
1996-07-08  0:00   ` Brian Rogoff
1996-07-11  0:00     ` Norman H. Cohen
1996-07-11  0:00       ` Magnus Kempe
1996-07-11  0:00         ` Robert Dewar
1996-07-09  0:00   ` Jon S Anthony
1996-07-09  0:00     ` Robert Dewar
1996-07-09  0:00     ` Robert Dewar
1996-07-09  0:00   ` Jon S Anthony
1996-07-09  0:00     ` Robert Dewar
1996-07-10  0:00   ` Ronald Cole
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00     ` Richard Kenner
1996-07-11  0:00   ` Jon S Anthony
1996-07-11  0:00   ` Jon S Anthony
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00   ` Jon S Anthony
1996-07-11  0:00     ` Robert Dewar
1996-07-15  0:00       ` Mark A Biggar
1996-07-15  0:00         ` Robert Dewar
1996-07-11  0:00     ` Tucker Taft
1996-07-17  0:00       ` Brian Rogoff
1996-07-12  0:00     ` Jon S Anthony
1996-07-12  0:00       ` Robert Dewar
1996-07-15  0:00     ` Jon S Anthony
1996-07-15  0:00       ` Robert Dewar
1996-07-12  0:00   ` Brian Rogoff
1996-07-16  0:00     ` Magnus Kempe
1996-07-14  0:00   ` Ronald Cole
1996-07-14  0:00     ` Robert Dewar
1996-07-15  0:00   ` Jon S Anthony
1996-07-15  0:00     ` Robert Dewar
1996-07-16  0:00   ` Brian Rogoff
1996-07-24  0:00 ` Jon S Anthony
1996-07-25  0:00 ` Fergus Henderson
1996-07-25  0:00   ` David Kristola
1996-07-26  0:00     ` Robert A Duff
1996-07-30  0:00       ` Thomas Wolff
1996-07-30  0:00         ` Robert A Duff
1996-07-30  0:00       ` David Kristola
1996-07-26  0:00   ` Robert A Duff
1996-07-26  0:00     ` Fergus Henderson
1996-07-28  0:00       ` Robert A Duff
1996-07-28  0:00         ` Fergus Henderson
1996-07-25  0:00 ` Jon S Anthony
  -- strict thread matches above, loose matches on Subject: below --
1996-07-05  0:00 tmoran
1996-07-06  0:00 ` Robert A Duff
1996-07-15  0:00 tmoran
1996-07-15  0:00 ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox