How to assign a record within a nested aggregate in Ada

时间:2018-10-17 12:52:32

标签: ada

Im trying to initialise a record variable that itself contains a nested record using an aggregate but can't seem to get the syntax right. Any help appreciated.

with Step; use Step;

package Pattern is   

   -- ADT
   type Pattern_Type is tagged private;

   -- ADT components
  type Bars_Type is private; 

  -- ADT instance methods
  function Tempo (This: Pattern_Type) return Integer;       
  function Bars (This: Pattern_Type)  return Bars_Type;    
  function Get_Next_Step(This: Pattern_Type ) return Step_Type;   

  -- Static methods
  function Get_Basic_Beat return Pattern_Type;

private

  type Bars_Type is range 0..2;                  
  Number_Of_Steps : constant Natural := 32;

  type Active_Step_Type is mod Number_Of_Steps;   
  type Steps_Type is array( Active_Step_Type ) of Step_Type;


  type Pattern_Type is tagged record
    Tempo             : Integer range 40..400;
    Bars              : Bars_Type := 1;
    Steps             : Steps_Type;
    Active_Step       : Active_Step_Type := 1;
  end record;                   

  -- Package variable
 Basic_Beat : Pattern_Type := 
   ( Tempo => 125, 
     Steps => Steps_Type'(1..31 => Step_Type'(Instrument => 'K', Velocity => 127, Offset => 0, Active => True)), 
     others => <> );

end Pattern;

...and within steps.ads

package Step is

  -- ADT
  type Step_Type is tagged private;      

  -- ADT instance methods
  function Instrument (This : Step_Type) return Character;
  function Velocity (This : Step_Type) return Integer;
  function Offset (This : Step_Type) return Integer;
  function Active (This : Step_type) return Boolean;

private   

  type Step_Type is tagged record            
    Instrument  : Character := ' ';      
    Velocity    : Integer := 0; 
    Offset      : Integer := 0;      
    Active      : Boolean := false;
  end record;

end Step;

I get this build error in GPS expected private type "Step_Type" defined at step.ads:4, found a composite type

Ive tried various combinations of the Step_Type'... line

1 个答案:

答案 0 :(得分:5)

您可以根据自己的需要选择不同的选项:

  1. 将Step_Type的记录实现公开,以便其他无关的软件包可以使用它。显然这会破坏封装。
  2. 添加一个可以基于Step包的公共区域中的输入参数创建Step_Type的函数。这是我个人首选的方法。
  3. 使Pattern成为Step的子包(提供对私有部分的可见性)。我通常仅在层次结构上有意义时才这样做。
  4. 将Step_Type和Pattern_Type放在同一包中。然后,它们的私有部分也将彼此可见。我也只会在对设计布局合理的情况下这样做。

第2个示例:

'Table2'