comp.lang.ada
 help / color / mirror / Atom feed
From: "Daniel Wengelin" <dawe@celsiustech.se>
Subject: Who is right? Gnat or http://www.adahome.com/articles/1998-02/ar_lessons95.html?
Date: 1999/04/07
Date: 1999-04-07T06:41:33+00:00	[thread overview]
Message-ID: <01be80c2$114e0890$c24d3a8b@m04w0588> (raw)



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). In practice, accessibility rules force the use of global
variable which makes multi-tasking very delicate. The only alternative is
to use generic parameters (parameterized methods), with the inconvenience
that parameterized methods are not inheritable. 

Example:

The following looks nice, but it will not compile! 

     generic
       type Item is private;
     package List is
        type Object is ...
        ...
        type Action is access procedure Action(
           The_Item : in out Item;
           Stop_Iterating : out Boolean);
        procedure Iterate (
           The_Action : Action
           Through : in List.Object);
        ...
     end List;
     
     with List, Student;
     package Student_List is
       new List (Item => Student.Object);
     
     with Student_List;
     function Retrieve_Student (With_Name : Student.Name;
                                From : Student_List.Object) return
Student.Object is

        The_Student_Found : Student.Object;

        procedure Find (The_Student : in out Student.Object;
                        Stop_Iterating : out Boolean) is
        begin
           if Student.Name_Of (The_Student) = With_Name then
              The_Student_Found := The_Student;
              Stop_Iterating := True;
           else
              Stop_Iterating := False;
           end if;
        end Find;

     begin
        Student_List.Iterate (The_Action => Find'Access,   -- Compilation
Error
                              Through => From);            
        return The_Student_Found;
     end Retrieve_Student;
------------------------------------- END COPY FROM ADAHOME
---------------------------------


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?

thanks.

  Daniel






             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 Daniel Wengelin [this message]
1999-04-07  0:00 ` Who is right? Gnat or http://www.adahome.com/articles/1998-02/ar_lessons95.html? Tucker Taft
1999-04-07  0:00 ` David C. Hoos, Sr.
1999-04-07  0:00   ` Daniel Wengelin
1999-04-07  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