1

I would like to make a procedure to accept generic constrained arrays i.e. both ecgReadings and eegReadings:

Types declarations:
   subtype ecgReadingsSize is Natural range 1..3;
   subtype eegReadingsSize is Natural range 1..10;
   subtype eegReading is Natural range 0..1; -- eegRReading is 0 or 1
   subtype ecgReading is Natural range 2..600; -- Max heart rate 220

   
   type ecgReadings is array (ecgReadingsSize) of ecgReading;
   type eegReadings is array (eegReadingsSize) of eegReading;
   type eegPartialSums is array (eegReadingsSize) of Natural;

Trying to make generic procedure:

package iocontroller is

   
   generic 
      type ItemType is private;
      type Index_Type is (<>);  -- Any discrete type
      type Array_Type is array (Index_Type range <>) of ItemType;
  procedure startIO(inFileName : String; outFileName : String; List: 
  Array_Type);

Testing generic prodcure

procedure Main is --change name

type MyArray is array (Natural range <>) of Natural;

procedure ecgIO is new startIO(
        ItemType => Natural,
        Index_Type => Natural,
        Array_Type => MyArray
    );
  • I’m sorry, but - you need to make clear what your problem actually is. Especially since your 'testing generic procedure' fragment compiles OK. Have you seen the help about [mcve]? – Simon Wright Jul 05 '20 at 16:15

1 Answers1

4

I think you just need to constrain the generic type parameter Array_Type of Start_IO (see example below).

Note: Although it's not mandatory, you might, in this case, want to declare types instead of subtypes to prevent accidental (implicit) conversions from e.g. ECG_Reading to EEG_Reading, i.e. declare

type ECG_Reading_Index is range 1 .. 3;
type ECG_Reading       is range 2 .. 600;

instead of

subtype ECG_Reading_Index is Natural range 1 .. 3;
subtype ECG_Reading       is Natural range 2 .. 600;

and similar for EEG_Reading_Index and EEG_Reading.

But apart from that:

main.adb

with IO_Controller;

procedure Main is
   
   subtype ECG_Reading_Index is Natural range 1 .. 3;
   subtype ECG_Reading       is Natural range 2 .. 600;
   type    ECG_Readings      is array (ECG_Reading_Index) of ECG_Reading;
   
   procedure Start_ECG_IO is new IO_Controller.Start_IO
     (Item_Type  => ECG_Reading,
      Index_Type => ECG_Reading_Index,
      Array_Type => ECG_Readings);   
   
   
   subtype EEG_Reading_Index is Natural range 1 .. 10;
   subtype EEG_Reading       is Natural range 0 .. 1;
   type    EEG_Readings      is array (EEG_Reading_Index) of EEG_Reading;
   
   procedure Start_EEG_IO is new IO_Controller.Start_IO
     (Item_Type  => EEG_Reading,
      Index_Type => EEG_Reading_Index,
      Array_Type => EEG_Readings);
   

   ECG : ECG_Readings := (others => <>);
   EEG : EEG_Readings := (others => <>);
   
begin
   Start_ECG_IO ("ecg_in", "ecg_out", ECG);
   Start_EEG_IO ("eeg_in", "eeg_out", EEG);
end Main;

io_controller.ads

package IO_Controller is   
   
   generic 
      type Item_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type) of Item_Type;   --  remove "range <>"
   procedure Start_IO
     (FileName_In  : String; 
      Filename_Out : String;
      List         : Array_Type);
 
end IO_Controller;
DeeDee
  • 5,654
  • 7
  • 14
  • Yes, I see how this way is much cleaner and simpler to follow. Thank you ever so much for your help! SPARK Ada is a very challenging yet rewarding language. God bless :) – Glenn McAvoy Jul 06 '20 at 11:30