1

I want to realize queue manager example from "Ada Distilled" (2005) adapted for my job. Minimal needed code:

package1.ads:

    with Ada.Finalization;
    use Ada;


        generic
           type Element is tagged private;
        package Lists is
           use Ada.Finalization;
           type Item is new Element with private;
           type Item_Reference is access all Item'Class;
           type Forward_List is new Controlled with private;
           function Init return Forward_List;
           function Is_Empty(Self:Forward_List'Class) return Boolean;
           procedure Add(Self:in out Forward_List;I:in Item'Class);

        private
           type Item is new Element with null record;       
           type Forward_List is new Controlled with
              record
                .........
              end record;
           type Forward_List_Reference is access all Forward_List;
           overriding procedure Initialize(List:in out Forward_List);
           overriding procedure Finalize(List:in out Forward_List);
        end Lists;

Package body:

package body Lists is

 function Init return Forward_List is
      FL_new:Forward_List_Reference:=new Forward_List;
   begin
      return FL_new.all;
   end Init;

   function Is_Empty(Self:Forward_List'Class) return Boolean is
   begin
      return Self.Count=0;
   end Is_Empty;


   procedure Add(Self:in out Forward_List;I:Item'Class) is
   begin
      null;
   end Add;

   procedure Finalize(Node:in out Forward_List_Node) is
   begin
      null;
   end Finalize;

   overriding procedure Initialize (List:in out Forward_List) is
   begin
      null;
   end Initialize;

   overriding procedure Finalize (List:in out Forward_List) is
   begin          
      null;
   end Finalize;

end Lists;

Main.adb (using this package):

with Lists;
with GNAT.Strings;
with Ada.Finalization;use Ada.Finalization;
procedure main is
   use GNAT.Strings;
   type Temp is new Controlled with
      record
         Name:Integer;
      end record;
   package Temp_List is new Lists(Temp);
   FL:Temp_List.Forward_List:=Temp_List.Init;

   Instance:Temp:=Temp'(Controlled with
                        Name => 60);
begin
   Temp_List.Add(Self => FL,
                 I    => Instance);
end main;

Result:

Builder results
E:\ADA\test_containers\20160303\main.adb
15:11 expected an access type with designated type "Item'class" defined at lists.ads:10, instance at line 9
found type "Temp"

I wanted to solve this problem all the week,read literature about this(packages,tagged records,class-wide types and generics), and cant understand the problem.

To make this code work I modified(replaced Item'Class by Element) package spec file to: package2.ads:

    with Ada.Finalization;
    use Ada;

    generic
       type Element is private;
    package Lists is
       use Ada.Finalization;
       type Forward_List is new Controlled with private;
       function Init return Forward_List;
       function Is_Empty(Self:Forward_List'Class) return Boolean;
       procedure Add(Self:in out Forward_List;I:in Element);

    private
       type E_Reference is access all Element;
       type Forward_List is new Controlled with
          record
            .........
          end record;
       type Forward_List_Reference is access all Forward_List;
       overriding procedure Initialize(List:in out Forward_List);
       overriding procedure Finalize(List:in out Forward_List);
    end Lists;

And then code "main.adb" start well.

So that's my question: How can I work with the first specification (example from "Ada Distilled" (package1))?

Lii
  • 11,553
  • 8
  • 64
  • 88
George
  • 78
  • 8
  • 1
    I expect I’m wrong, but I’m not sure that that example has ever been actually usable. Couldn’t you just use `Ada.Containers`? (even if you’re stuck at Ada 2005, you can implement a queue using a vector). – Simon Wright Mar 04 '16 at 09:33
  • Simon,thanks for answer!One of my tasks is to make folders tree of disk(with acls list and other characteristics), that's why i decided to create my own containers for it. So I made it without type conversion. And I want to know, whether that example(package1) usable or not. It is too hard to believe for me, that that example written in book is not usable. (sorry for my bad english) – George Mar 04 '16 at 12:43
  • I posted this problem on [comp.lang.ada](https://groups.google.com/forum/?hl=en-GB#!topic/comp.lang.ada/hDKe3oENZNw) and the general opinion is that the book is wrong. – Simon Wright Mar 04 '16 at 21:40
  • @SimonWright thanks a lot, you've deleted this problem from my head, I can do my job with no doubts. – George Mar 05 '16 at 03:50

1 Answers1

0

Simon Wright provided an answer in comments:

I posted this problem on comp.lang.ada and the general opinion is that the book is wrong.

I'll continue my job with Element in methods(according to package2 in my question).

George
  • 78
  • 8