Using the GNAT compiler, when I try to compile or check semantic on the following files, I get some variation of the error package "Foo" does not allow a body. I’m new to the language and can’t find an answer to this seemingly basic problem anywhere on the Internet. Please advise.
foo.ads
package Foo is
type Shape_Enum is (Circle, Triangle, Rectangle);
end Foo;
foo.adb
package body Foo is
procedure Foo is
MyShape : Shape_Enum;
begin
MyShape := Rectangle;
end Foo;
end Foo;
A package is only allowed to have a body if the specification includes something that requires a body. (This avoid problems where an optional body might accidentally be left out of a build.)
You have a procedure in the body of the package (
Foo.Foo), but there’s no way to call it.If you add a declaration:
to the specification, that should (a) fix the error, and (b) permit the procedure to be called by clients of the package. Or you can use
pragma Elaborate_Body;to require it to have a body if you don’t want the procedure to be visible to clients.Incidentally, there’s nothing special about a procedure with the same name as the package that contains it (unlike in C++, where such a function is a constructor for the containing class). It would probably be clearer to use a different name.
See section 7.2 of the Ada Reference Manual (I’m using a recent draft of the 2012 standard):