2
votes

I searching a way to use Put() function with a custom type I create. How could I do this ?

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure main is
   type count is range -2..2 ;
begin
      Put(counter);
end main;

this is what I got :

Builder results
    C:\Users\***********\Desktop\ada project\src\main.adb
        26:11 expected type "Standard.Integer"
        26:7 no candidate interpretations match the actuals:
        26:7 possible missing instantiation of Text_IO.Integer_IO
1

1 Answers

5
votes

You're missing the instance, Counter, and there's no subprogram Put that takes a parameter of type Count. Some options:

  • Option 1 - Use the Image attribute.
with Ada.Text_IO;

procedure Main is
   type Count is range -2 .. 2;   
   Counter : Count := 1;   
begin
   Ada.Text_IO.Put (Counter'Image);   -- Counter'Image returns a String
   -- or 
   Ada.Text_IO.Put (Count'Image (Counter));
end Main;
  • Option 2 - Cast to type Integer.
with Ada.Integer_Text_IO;

procedure Main is
   type Count is range -2 .. 2;   
   Counter : Count := 1;    
begin
   Ada.Integer_Text_IO.Put (Integer (Counter));  
end Main;
  • Option 3 - Define a subtype instead of a type.
with Ada.Integer_Text_IO;

procedure Main is
   subtype Count is Integer range -2 .. 2;   
   Counter : Count := 1;  
begin
   Ada.Integer_Text_IO.Put (Counter);  
end Main;
  • Option 4 - Instantiate generic package Ada.Text_IO.Integer_IO.
with Ada.Text_IO;

procedure Main is

   type Count is range -2 .. 2;   
   Counter : Count := 1; 

   package Count_Text_IO is
      new Ada.Text_IO.Integer_IO (Count);   

begin
   Count_Text_IO.Put (Counter);   
end Main;