Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada "Compilation Unit Expected" error

I'm trying to declare a new type so I can pass an array as an argument to a procedure. It looks like this:

type Arr_Type is array(1..1000) of String;

procedure proceed(Arg1: in Arr_Type) is
begin
<program body>
end

Whenever I try and compile this, I receive a "compilation unit expected" error. If I remove the type declaration, I no longer get the error, but I obviously need it and I get an error if I put it anywhere else in the file. I'm a little new to Ada so I'm not entirely sure what's happening here.

like image 300
Christian Baker Avatar asked Feb 09 '23 00:02

Christian Baker


1 Answers

A program in Ada has to be divided into compilation unit (procedure, function or package). The type declaration has to be contained in a unit so you could wrap these in a procedure:

procedure Main is

  type Arr_Type is array(1..1000) of String;

  procedure proceed(Arg1: in Arr_Type) is
  begin
   <program body>
  end proceed;

begin

   call to proceed

end Main;

If you already have a program calling proceed but want it on a separate file, you'll need a package instead. Then you create two files - a specification file (.ads) and a body file (.adb):

my_package.ads:

package My_Package is
  type Arr_Type is array(1..1000) of String;

  procedure proceed(Arg1: in Arr_Type);
end My_Package;

my_package.adb:

package body My_Package is

  procedure proceed(Arg1: in Arr_Type) is
  begin
   <program body>
  end Proceed;

end My_Package;

Then you can include this package as usual with with My_Package (and possible use)

like image 165
erlc Avatar answered Feb 19 '23 18:02

erlc