Failed to access Ada public parameter members

I am trying to write a general package, and one of the necessary operations is a checksum of data received on the bus. The type of record will be different, and this is a common parameter. However, any attempts to access the members of a common parameter cause a compilation error.

Error ... (Ada 95 GNAT 2009)

file.adb:XX no selector "Data" for private type "The_Transfer_Type" defined at file.ads:YY

Announcement...

generic
  type The_Transfer_Type is private;
  SIZE : Integer;
package CC_Test_Channel is
  function Checksum(Msg : The_Transfer_Type) return Integer;
end package

And the body ...

function Checksum(Msg : The_Transfer_Type) return Integer is
  Sum : Integer := 0;
begin
  -- calculate the checksum
  for i in 1 .. SIZE loop
    Sum := Sum + Integer(Msg.Data(i));
  end loop;
  return Sum;
end Checksum;
+5
source share
3 answers

When you indicate that a generic parameter is a private type, well, Ada assumes you mean this :-)

.. . " duck typed", , , . ( , Checksum , The_Transfer_Type , , Integer?)

- , , . :.

generic
   type The_Transfer_Type is private;
   with function Get_Checksummable_Data_Item
           (Msg : The_Transfer_Type;
            I   : Integer) return Integer;
   SIZE : Integer;

package CC_Test_Channel is
   function Checksum(Msg : The_Transfer_Type) return Integer;
end CC_Test_Channel;

:

function Checksum(Msg : The_Transfer_Type) return Integer is
   Sum : Integer := 0;
begin
   -- calculate the checksum
   for i in 1 .. SIZE loop
      Sum := Sum + Get_Checksummable_Data(Msg, I);
   end loop;
   return Sum;
end Checksum;

, Get_Checksummable_Data, The_Transfer_Type The_Transfer_Type.

, , - SIZE, Checksum() , CC_Test_Channel, :

with function Calculate_Checksum(Msg : The_Transfer_Type) return Integer;

.

...

+5

( , )

Ada (95 ) . ++, , Ada ( I/O).

Ada 'Write 'Read. , ( - ), , Ada.Streams.Root_Stream_Type. , , .

, -, , , , , ( "" ). , / . ( ).

+4
generic 
  type The_Transfer_Type is private; 
  ...

, , , The_Transfer_Type ( "" ). , , , .

Ada , . , is limited private. . , . .

Remove the "restricted" one and you can bind to it, but only types that can be assigned can be provided. On the other hand, you can define it as: type The_Transfer_Type is (<>)and then you can specify any integer or numbered type and get things like 'first. Going further, you could do type The_Transfer_Type is range <>, and you would be able to perform mathematical operations with integers, but could only have integer numeric types.

+3
source

All Articles