From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d0fa2610a6bea4ec X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.glorb.com!newsfeed0.kamp.net!newsfeed.kamp.net!feeder3.cambrium.nl!feed.tweaknews.nl!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Nested declares, constant size arrays and clarity References: Date: Thu, 04 Oct 2007 01:49:19 +0200 Message-ID: <87641n92tc.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:2yo+cYnpKmyB91YzcHyYq5JXHYY= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tele2 X-Trace: DXC=6B[Qe6P_h`3gC>1ZBi:o1:6`Y6aWje^Y:IK?3lUg:S]31B4<]HH1:n3;oCOVDLkV_>[3kPXPlo484 Xref: g2news2.google.com comp.lang.ada:2284 Date: 2007-10-04T01:49:19+02:00 List-Id: Mateusz Papiernik 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.