comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Who is right? Gnat or http://www.adahome.com/articles/1998-02/ar_lessons95.html?
Date: 1999/04/07
Date: 1999-04-07T00:00:00+00:00	[thread overview]
Message-ID: <F9t503.L4J.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 01be80c2$114e0890$c24d3a8b@m04w0588

Daniel Wengelin (dawe@celsiustech.se) wrote:


: When looking around for some infomration about OOP I came across the
: article http://www.adahome.com/articles/1998-02/ar_lessons95.html.

: When skimming through, I noted the following section
: ----------------------------------------- COPY FROM ADAHOME
: ---------------------------------
: The ability to pass subprograms as parameters is one of the nicest features
: of Ada 95 (it didn't exist in Ada 83). But accessibility rules make it
: useless within a multi-tasking program. Indeed, access to a local procedure
: cannot be used as a parameter of a more globally declared procedure (e.g. a
: class method). ...
: ...
: I gave the code a try with Gnat 3.11 as follows.

: generic
:    type Item_T is private;
: package List is
:    type Node;
:    type Object is access Node;
:    type Node is record
:       I : Item_T;
:       Next:Object;
:    end record;

:    type Action_Proc is access procedure
:      (I : in out Item_T);
:    procedure Iterate
:      (The_Action : Action_Proc;
:       Through_List : Object);
: end List;

: ---------------------------
: package body List is
:    procedure Iterate
:      (The_Action : Action_Proc;
:       Through_List : Object)
:    is
:       Current : Object := Through_List;
:    begin
:       while Current /= null loop
:          The_Action(Current.I);
:          Current := Current.Next;
:       end loop;
:    end Iterate;
: end List;

: ------------------------

: procedure List_Test is

:    procedure Print (C: in out Character) is
:    begin
:       Ada.Text_Io.Put(C);
:       Ada.Text_Io.New_Line;
:    end Print;
:    package C_List is new List(Character);

:    The_List : C_List.Object := null;
: begin
:    for I in Character range 'a' .. 'e' loop
:       The_List := new C_List.Node'(I, The_List);
:    end loop;

:    C_List.Iterate (Print'Access, The_List);
: end List_Test;

: The above source compiled and ran without any problems. So, did I
: misunderstand or is Gnat wrong or is the article in AdaHome wrong?

The critical difference between your code and the code on AdaHome
is that your instantiation of the generic is at the same accessibility
level as the function named in the prefix to 'Access.  The code
in AdaHome might be able to work the same way, by moving their instantiation
so that it is inside the outer subprogram.  But there also may be reasons
why they didn't want the instantiation itself nested.

In any case, one way to address these "multitasking" problems is
to add an additional class-wide parameter through which additional
data can be passed through to a global function.

: thanks.

:   Daniel

--
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




      parent reply	other threads:[~1999-04-07  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-07  0:00 Who is right? Gnat or http://www.adahome.com/articles/1998-02/ar_lessons95.html? Daniel Wengelin
1999-04-07  0:00 ` David C. Hoos, Sr.
1999-04-07  0:00   ` Daniel Wengelin
1999-04-07  0:00 ` Robert Dewar
1999-04-07  0:00 ` Tucker Taft [this message]
replies disabled

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