views:

55

answers:

1

Hi I'm trying to create a new Float type with 6 digits precision. But it seems that I'm not able to use it. Here's the code from the package MyFloat_IO.ads

WITH Ada.Text_IO;

PACKAGE MyFloat_IO IS

TYPE MyFloat IS DIGITS 6 RANGE 0.0..Float'Last;
PACKAGE MyFloat_IO IS NEW Ada.Text_IO.Float_IO(MyFloat);

end MyFloat_IO;

and the main code:

WITH Ada.Text_IO;
WITH MyFloat_IO;
USE MyFloat_IO;
WITH Ada.Numerics;


PROCEDURE TEST2 IS

X : MyFloat := 3.5;
Y : CONSTANT := Ada.Numerics.Pi;
Z : MyFloat;


BEGIN -- main program
  Z := X * Y;
  MyFloat_IO.Put(Z);
  Ada.Text_IO.New_Line;
END TEST2;

On compiling I get the error message:

14. BEGIN -- main program
15.     Z := X * Y;
16.     MyFloat_IO.Put(Z);
                  |
    >>> "Put" not declared in "MyFloat_IO"

17.     Ada.Text_IO.New_Line;
18. END TEST2;

What am I doing wrong?

Thanks a lot...


Update: new code as per the suggestions of T.E.D:

  1. package MyFloat_I0.ads :

    WITH Ada.Text_IO;
    PACKAGE MyFloat_I0 IS
    TYPE Instance IS DIGITS 6 RANGE 0.0..Float'Last;
    PACKAGE MyFloat IS NEW Ada.Text_IO.Float_IO(Instance);
    end MyFloat_I0;
    
  2. and the main code :

    WITH Ada.Text_IO;
    WITH MyFloat_I0;
    use MyFloat_I0;
    WITH Ada.Numerics;
    
    
    PROCEDURE TEST2 IS
    
    
    X : Instance := 3.5;
    Y : CONSTANT := Ada.Numerics.Pi;
    Z : Instance;
    
    
    BEGIN -- main program
      Z := X * Y;
      MyFloat.Instance.Put(Z);
      Ada.Text_IO.New_Line;
    END TEST2;
    

On compilation I get:

MyFloat.Instance.Put(Z);
               |
    >>> "Instance" not declared in "MyFloat"
+2  A: 

What you are doing wrong is that you declared a package named MyFloat_IO (derived from Ada.Text_IO.Float_IO) inside of another package also named MyFloat_IO. In order to get at it the way you have things declared, you would have to call:

MyFloat_IO.MyFloat_IO.Put(Z);

I'm pretty sure it is possible to just derive a package from a generic by itself as a compilation unit. However, you might consider instead renaming the package MyFloat, the type something like Instance, and your IO package IO. That way, people looking to use it will say MyFloat.Instance for the type and MyFloat.IO for the IO package.

(Update answer)

I can see from the code you posted I must have confused you thoroughly. Here's what I had in mind (warning: not compiled).

package MyFloat is
   type Instance is digits 6 range 0.0..Float'Last; --'

   package IO is new Ada.Text_IO.Float_IO (Instance);
end MyFloat;

In other words, the package is named MyFloat, the type is named Instance, and the IO package is named IO. When called from outside the package, the type is MyFloat.Instance, and the put routine is MyFloat.IO.Put.

Note that if you are using Gnat, you will have to rename the source file when you rename the package.

T.E.D.
Thanks for your help. I did what you wrote in the second paragraph of your answer i.e. now I have MyFloat_I0.Instance.Put(Z) but on compiling I get >>invalid prefix in selected component "INSTANCE". There is still something wrong. I have changed package MyFloat to Instance and renamed MyFloat_IO to MyFloat_I0 with an ending zero digit.
yCalleecharan
You wrote: MyFloat.Instance .But MyFloat doesn't exist anymore as we have renamed it to Instance. Or are you asking me to use a generic package? It's a bit unclear to me, Thanks.
yCalleecharan
`MyFloat` will be the package name. If `Instance` is the name of your type inside the package, then from outside the package you would refer to it as `MyFloat.Instance`.
T.E.D.
Thanks for clearing things out. I'm still not getting it right. I've put my new code at the bottom of my post.
yCalleecharan
Your first solution i.e MyFloat_IO.MyFloat_IO.Put(Z) works. 1 vote up. It would be great if I could to make your other solution work too.
yCalleecharan
Thanks for your very kind help. It works fine. Yes I was confused but now I can see the logic in what you wrote and it makes sense. This "example" is a very good illustration of package referencing for Ada beginners. I was confusing the package with the type as you have noticed.
yCalleecharan