我想知道是否可以初始化这个数组:
Type Some is array (1..5) of Integer;
SomeArray : Some := ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 );
以某种不同的方式,像这样:
SomeArray : Some := ( others => n );
有可能吗?
答案 0 :(得分:3)
除了“某些”现在是Ada 2012中的保留字这一事实外,您可以完全按照您的描述初始化该类型的数组。 E.g:
with Text_IO; use Text_IO;
procedure Agg_Init_Test is
type Some_Type is array (1.. 5) of Integer;
N : constant := 4;
Data : Some_Type := (others => N);
procedure Init_To (N : Integer) is
begin
Data := (others => N);
end Init_To;
procedure Init_Data (Data : out Some_Type; N : Integer) is
begin
Data := (others => N);
end Init_Data;
function Inc (Val : in out Integer) return Integer is
begin
Val := Val + 1;
return Val;
end Inc;
procedure Init_Seq(Data : out Some_Type; Start : Integer) is
N : Integer := Start;
begin
Data := (others => Inc(N));
end Init_Seq;
begin
Init_To(42);
Init_Data(Data, 2012);
Init_Seq(Data, 0);
for I of Data loop
Put_Line(Integer'Image(I));
end loop;
end Agg_Init_Test;