具有已声明索引数的形式数组类型(通用包)

时间:2018-08-07 06:27:21

标签: ada

通用包装规范:

generic
   type Real is digits <>;
   type Real_Arr is array (Integer range <>) of Real;
package Pack1 is
   function Gt (A : in Real; B : in Real_Arr) return Boolean;
end Pack1;

通用包主体:

package body Pack1 is
   function Gt (A : in Real; B : in Real_Arr) return Boolean
   is
     (for all X of B => (X > A));
end Pack1;

对于功能GtB的尺寸并不重要。

我该怎么做?

generic
   type Real is digits <>;
   --  Declare number of indexes?
   --  N : Positive;
   --  type Real_Arr is array (??? N ???) of Real;
package Pack1 is

并用于不同类型:

with Pack1;
package Math is
   --  Array types are outside of generic package!
   type Vector is array (Positive range <>) of Float;
   type Matrix is array (Positive range <>, Positive range <>) of Float;

   package Opv is new Pack1 (Float, 1, Vector);
   package Opm is new Pack1 (Float, 2, Matrix);
end Math;

我想这样做是因为函数Gt的实现对于具有任何索引和维度的数组都是相同的。

1 个答案:

答案 0 :(得分:1)

我假设您想给数字N,然后让包声明一个包含N值数组的函数。这样做:

generic
   type Real is digits <>;
   N : Positive;
package Pack1 is
   type Real_Arr is array (1 .. N) of Real;
   --  ...
end Pack1;

请注意,数组类型不是通用参数。而是由包根据给定的通用参数N声明类型。

当然,如果您的数组类型已经在软件包外部,那么您也可以将数组类型用作通用参数。