如何在Ada中的嵌套聚合中分配记录

Ben*_*enC 1 ada

我试图初始化一个记录变量,它本身包含一个使用聚合的嵌套记录,但似乎无法使语法正确.任何帮助赞赏.

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;
Run Code Online (Sandbox Code Playgroud)

......并在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;
Run Code Online (Sandbox Code Playgroud)

我在GPS中遇到此构建错误 expected private type "Step_Type" defined at step.ads:4, found a composite type

我已尝试过Step_Type'...行的各种组合

Jer*_*ere 5

根据您的需求,您有不同的选择:

  1. 使Step_Type的记录实现公开,以便其他不相关的包可以使用它.这显然打破了封装.
  2. 添加一个可以根据Step包的公共区域中的输入参数创建Step_Type的函数.这是我个人喜欢的方法.
  3. Make Pattern是Step的子包(提供对private部分的可见性).我通常只在层次上有意义的时候这样做.
  4. 将Step_Type和Pattern_Type放在同一个包中.然后他们的私人部分也相互可见.如果我对如何布置设计有意义,我也只会这样做.

#2的一个例子:

with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is

    package Step is

      -- ADT
      type Step_Type is tagged private;      

      function Make
        (Instrument : Character;
         Velocity   : Integer;
         Offset     : Integer;
         Active     : Boolean)
         return Step_Type;

    private   

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

      function Make
        (Instrument : Character;
         Velocity   : Integer;
         Offset     : Integer;
         Active     : Boolean)
         return Step_Type
      is (Instrument => Instrument, 
          Velocity   => Velocity, 
          Offset     => Offset, 
          Active     => Active);

    end Step;

    use Step;
    package Pattern is   

       -- ADT
       type Pattern_Type is tagged private;

       -- ADT components
      type Bars_Type is private; 



    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 => (others => Make('K',127,0,True)), 
         others => <> );

    end Pattern;


begin
  Put_Line("Hello, world!");
end Hello;
Run Code Online (Sandbox Code Playgroud)