1

I want to know if it is possible to initialise this array:

Type Some is array (1..5) of Integer; SomeArray : Some := ( 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 ); 

In some different way, like this:

SomeArray : Some := ( others => n ); 

Is it possible?

1
  • 1
    Yes; but why didn't you just try it? Commented Sep 24, 2012 at 14:20

1 Answer 1

4

Except for the fact that "Some" is now a reserved word in Ada 2012, you can initialize that kind of array exactly as you describe. 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; 
Sign up to request clarification or add additional context in comments.

3 Comments

OOC, has there been any discussion of adding something like lambdas? That would reduce the answer to his question to a one-liner.
I was thinking an Index attribute would be kinda nice, it could also be multiple-dimension aware like the Range attribute.
You can see that I just threw this together off the top of my head (the sequencing part was actually added as an edit). It would be interesting to really dig in and see if there was a better way to do this--while I'm well ahead in adapting 2012 features compared to past revisions, there's still a lot out there I haven't looked at yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.