3 ms·
This is untrue. This is an Ada declaration for a fixed-size array of Foo with indices 1 to 10 (inclusive): type Foo_Array is array (Integer range 1..10) of
by xlq 15y ago
This is untrue. This is an Ada declaration for a fixed-size array of Foo
with indices 1 to 10 (inclusive):
type Foo_Array is array (Integer range 1..10) of Foo;
This is an array indexed by an enumeration:
type Foo_Array is array (Some_Enum) of Foo;
But dynamically sized arrays can also be declared:
type Foo_Array is array (Integer range <>) of Foo;
Note that such an array has a dynamic lower bound and a dynamic upper bound. If
I create an instance of such an array and pass it to a procedure:
declare
My_Array: Foo_Array (3..10)
begin
Do_Something (My_Array);
end;
then the procedure Do_Something receives both bounds of the array, 3 and 10,
which it can read with the 'First and 'Last attributes.
A declaration like the one above is typically done on the stack (although it
doesn't have to be). An array can be allocated on the heap with "new", with,
IIRC, the following syntax:
new Foo_Array (3..10)
- tedunangst 15y agoThanks for the clarification. Can you create an array with variable size though? new Foo_Array(x..y) That's what I couldn't figure out how to do last I tried.
- xlq 15y agoYes. What you've written is exactly how to allocate an array of dynamic size on the heap. You can also do: declare My_Array: Foo_Array(X..Y); begin ... end;
- tedunangst 15y agoHmm, I remember it being very hard, but it seems I've lost the specifics. My bad. In any case, Ada is not a language I'm excited to work with, but Go could be one.