Question

I am having a problem while writing a package for doubly circular linked lists in ada. I am specifically writing a function that will take my DCLL and return the contents of it in array form. In my spec file I created the type like this

type ListArray is array(Integer range <>) of Integer;

My problem is, I keep getting "length check failed" errors when the client calls my package. Here is the client program(part)

procedure tester is

   LL : sorted_list.List;
   larray : sorted_list.ListArray(1..sorted_list.length(LL));

   begin
   sorted_list.Insert(LL, 5);
   larray:= sorted_list.toArray(LL);
   end;

I know this is failing because when I define larray and set it to the length, the length is 0 because LL doesn't have anything in it yet. In java I would just initialize the array in the body of the code after the insertion but it seems in ada I cannot do that (or at least I don't know how) Is there anyway to create an array in ada without defining the bound and then define the bounds in the body after insertion?

I hope I have explained my problem well enough for you to understand. Thanks.

Was it helpful?

Solution

procedure tester is

    LL : sorted_list.List;

begin
    sorted_list.Insert(LL, 5);
    declare
        larray : sorted_list.ListArray(1..sorted_list.length(LL));
    begin
        larray := sorted_list.toArray(LL);
        -- code that uses larray must be in this block
    end;
end;

or

procedure tester is

    LL : sorted_list.List;

begin
    sorted_list.Insert(LL, 5);
    declare
        larray : sorted_list.ListArray := sorted_list.toArray(LL);
            -- no need to specify the bounds, it will take them from the bounds
            -- of the result returned by toArray
    begin
        -- code that uses larray must be in this block
    end;
end;

A couple things to note: (1) The declarations in the block (beginning with declare) are evaluated at the point where a statement would be executed (in fact, a block is one kind of statement), so it will use the LL that has been set up at that point. (2) The variable larray is visible only inside the block where you declared it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top