comp.lang.ada
 help / color / mirror / Atom feed
From: Dmitry A. Kazakov <mailbox@dmitry-kazakov.de>
Subject: Re: Problem With Self-Referential Access-to-Subprogram Type
Date: Tue, 04 Nov 2003 11:04:54 +0100
Date: 2003-11-04T11:04:54+01:00	[thread overview]
Message-ID: <80teqv099lspc5d4osf2gmu7cld46i0lvb@4ax.com> (raw)
In-Reply-To: c8f570aa.0311030858.5425fc0d@posting.google.com

On 3 Nov 2003 08:58:45 -0800, taashlo@cyberdude.com (Tad Ashlock)
wrote:

>(Background information: I'm an Ada newbie using GNAT 3.15p on Windows
>2000.)
>
>In the demonstration code below, State1 compiles and works fine, but the
>declaration of State2 generates an error (premature use of "State2").
>
>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 ^

   type State2 is access procedure (Tester : in out Test_Type'Class;
                                    Resultant_State : access State2);

>private
>   type Test_Type is
>     tagged record
>        Dummy : Integer;
>     end record;
>end Test;
>
>I started off by calling functions via variables of type State1.  But
>then I needed to make changes to the Tester parameter within the
>functions (e.g. calling Set_Dummy).  So I decided to change to calling
>procedures via variables of type State2.  But this resulted in error
>messages (premature use of "State2").
>
>Technically, I could still use an access-to-function type (State1) and
>just change the Tester parameter to be an access to Test_Type'Class *and*
>make all variables and constants of Test_Type aliased *and* pass them
>using the 'Access attribute *and* change certian packages to be generic
>*and* carefully instantiate them in the right scope to avoid the
>"non-local pointer cannot point to local object" errors.
>
>But that doesn't feel right.  The subprograms being accessed *do* have
>side-effects and *should* be procedures rather than functions.  Changing
>to the parameter to an access type to get around the "functions have
>input parameters only" rule seems like cheating.  Besides, the changes
>listed above seem like alot of hassle for very little return.
>Effectively, I just want to change to returning the value via an "out"
>parameter rather than via a function result.
>
>Am I missing something obvious?

You are using tagged types, why then you don't use dispatching calls?
Access to subprogram is a sort of polymorphism of poor man. Perhaps
you can review your design with that respect. Namely, to put the
knowledge of what should be done into Test_Type'Class. Probably it
would imply multiple dispatch/inheritance, then for both there are
workarounds in Ada.

---
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de



  reply	other threads:[~2003-11-04 10:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-03 16:58 Problem With Self-Referential Access-to-Subprogram Type Tad Ashlock
2003-11-04 10:04 ` Dmitry A. Kazakov [this message]
2003-11-04 13:52   ` Tad Ashlock
2003-11-04 14:24     ` Dmitry A. Kazakov
2003-11-04 14:52       ` Marius Amado Alves
2003-11-05  8:31         ` Dmitry A. Kazakov
2003-11-05 11:27           ` Marius Amado Alves
2003-11-05 13:33             ` Dmitry A. Kazakov
2003-11-05 14:02               ` Marius Amado Alves
2003-11-05 17:49             ` Adam Beneschan
2003-11-05 18:55               ` Marius Amado Alves
2003-11-05 22:54                 ` Tad Ashlock
2003-11-06 17:45                   ` Warren W. Gay VE3WWG
  -- strict thread matches above, loose matches on Subject: below --
2003-11-03 17:32 amado.alves
2003-11-04 14:02 ` Tad Ashlock
replies disabled

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