Question

I am new to Ada and am having some issues. I have created a generic Date package:

Date.ads:

GENERIC

PACKAGE Date IS
  TYPE MonthName IS (January, February, March, April, May, June, July, August,
   September, October, November, December);

  TYPE DateRec IS RECORD
    Month: MonthName;
    Day: Integer RANGE 1..31;
    Year: Integer;
  END RECORD;

  PROCEDURE PrintDate(Adate: IN DateRec);
END Date;

Date.adb:

WITH Ada.Text_IO;
USE Ada.Text_IO;

PACKAGE BODY Date  IS

PACKAGE MonthNameIO IS NEW Ada.Text_IO.Enumeration_IO(MonthName);
USE MonthNameIO;

PACKAGE IntegerIO IS NEW Ada.Text_IO.Enumeration_IO(Integer);
USE IntegerIO;

PROCEDURE PrintDate(Adate: IN DateRec) IS
  BEGIN
    Put(Adate.Month);
    Put(Adate.Day,3);
    Put(",");
    Put(Adate.Year, 5);
    New_Line;
  END PrintDate;
END Date;

I want to be able to use "DateRec" in a program, but encounter errors when I attempt to compile. The calling code:

WITH Date;
...
PACKAGE Date_Stack IS NEW Gstack(StackSize, DateRec);

I get the following errors when I compile:

genstack.adb:176:57: "DateRec" is not visible (more references follow)
genstack.adb:176:57: non-visible declaration at date.ads:7

What am I missing?

Ok, now the problem I am having is when I attempt to use an object:

TempDate: Date.DateRec;
...
Get(TempDate.Month);

The compile now gives me:

genstack.adb:201:25: missing argument for parameter "Item" in call to "Get" declared at a-tienio.ads:65, instance at line 183             
Was it helpful?

Solution

Your Date package has no generic parameters, so there's probably no point in making it generic.

If you drop the GENERIC keyword, it will likely work (I haven't actually tried it myself).

If you really do want it to be generic, then you'll need to instantiate it before you can use it. Date is not a package; it's a generic package.

 generic
 package Date is
 ...
 end Date;

 package MyDate is new Date;

Now MyDate is a (non-generic) package, and you can refer to MyDate.DateRec.

By making it generic with no generic parameters, you can create multiple instantiations, each of which is a distinct package, so that MyDate.DateRec and YourDate.DateRec are distinct types. It's not clear that that's worthwhile.

Normally a generic package has one or more parameters, so that different instances operate on different types. You even have examples of that in your code: Ada.Text_IO.Enumeration_IO is generic, and your MonthNameIO is a specific instance of that generic package.

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