From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8522260ffbf09d84 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-04 02:03:04 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!tar-alcarin.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: Problem With Self-Referential Access-to-Subprogram Type Date: Tue, 04 Nov 2003 11:04:54 +0100 Message-ID: <80teqv099lspc5d4osf2gmu7cld46i0lvb@4ax.com> References: NNTP-Posting-Host: tar-alcarin.cbb-automation.de (212.79.194.111) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1067940182 45220085 212.79.194.111 (16 [77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:2029 Date: 2003-11-04T11:04:54+01:00 List-Id: 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