From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Nested declares, constant size arrays and clarity
Date: Thu, 04 Oct 2007 01:49:19 +0200
Date: 2007-10-04T01:49:19+02:00 [thread overview]
Message-ID: <87641n92tc.fsf@ludovic-brenta.org> (raw)
In-Reply-To: fe1748$it1$1@nemesis.news.tpi.pl
Mateusz Papiernik <mati@maticomp.net> writes:
> Hey!
>
> I'm starting off with Ada95. I'm having some problems with clarity of
> my solution, which - I think - is not as good as I would like to.
>
> I've got my root procedure in which I read some number from the
> standard input. Then I create an array of MyType with size read before.
>
> procedure mxp is
> ... -- some record type declarations
> ... -- some *functions*
> begin
> ... -- read size
> declare
> type MyTypeArray is array (1...sizegiven) of MyType;
> MyArray: MyTypeArray;
> ... -- some *functions* doing something with newly made array
> begin
> ... -- these function calls
> end;
> end;
>
> The problem is, I don't really like the idea of declaring some
> functions in the root procedure declaration, and some others in nested
> declare section. Due to that I've got function implementations in two
> places, which somehow feels awkward to me.
>
> Is there any way to accomplish the same without that nesting _and_
> without building external module/using OOP/pointers?
>
> Maybe there is a way to define general array type of unknown size,
> then declare functions doing something with it in the topmost
> declaration section, and then only somehow initialise the array with
> given size?
Yes, it is called an "unconstrained array type". Here is how you
would do it:
procedure mxp is
type My_Record_Type is record ... end record;
type My_Array_Type is array (Positive range <>) of My_Record_Type;
-- the type is unconstrained
Size : constant Natural := Get_From_Keyboard;
My_Array : My_Array_Type (1 .. Size); -- the array is constrained
begin
-- Call the functions
end mxp;
Note: the array type uses Positive as the index type; the Size is a
Natural. Positive and Natural are both subtypes of Integer and so are
compatible with one another:
subtype Positive is Integer range 1 .. Integer'Last;
subtype Natural is Integer range 0 .. Integer'Last;
If the user types zero for the size, then My_Array will have the
bounds 1 .. 0. THIS IS OKAY!!! In that case, looping over the array
will simply do nothing. You normally loop over an array like this:
for K in My_Array'Range loop
-- ...
end loop;
PS. What I described above is exactly how the predefined type String
works; it is an unconstrained array of Character's. Look at the
standard subprograms that operate on Strings for more inspiration.
HTH
--
Ludovic Brenta.
next prev parent reply other threads:[~2007-10-03 23:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-03 22:58 Nested declares, constant size arrays and clarity Mateusz Papiernik
2007-10-03 23:49 ` Ludovic Brenta [this message]
2007-10-04 8:49 ` Mateusz Papiernik
2007-10-04 11:33 ` Ludovic Brenta
2007-10-04 12:45 ` Dmitry A. Kazakov
2007-10-04 13:11 ` Mateusz Papiernik
2007-10-05 5:00 ` Jeffrey R. Carter
2007-10-05 4:58 ` Jeffrey R. Carter
2007-10-05 7:38 ` Dmitry A. Kazakov
2007-10-05 17:08 ` Jeffrey R. Carter
2007-10-05 4:54 ` Jeffrey R. Carter
2007-10-04 0:20 ` Jeffrey R. Carter
2007-10-04 8:51 ` Mateusz Papiernik
2007-10-05 2:45 ` Steve Whalen
2007-10-04 0:23 ` anon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox