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,fe5e779eaf4ecf02 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!inka.de!rz.uni-karlsruhe.de!news.uni-stuttgart.de!not-for-mail From: Stefan Bellon Newsgroups: comp.lang.ada Subject: Returning ranges from a function (was: How to loop (elegant) throug a vector ?) Date: Fri, 20 Jun 2008 19:12:37 +0200 Organization: Comp.Center (RUS), U of Stuttgart, FRG Message-ID: <20080620191237.5971480b@cube.tz.axivion.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: infosun2.rus.uni-stuttgart.de 1213981940 28005 129.69.226.23 (20 Jun 2008 17:12:20 GMT) X-Complaints-To: news@news.uni-stuttgart.de NNTP-Posting-Date: Fri, 20 Jun 2008 17:12:20 +0000 (UTC) X-Newsreader: Claws Mail 3.4.0 (GTK+ 2.12.10; i486-pc-linux-gnu) X-URL: http://www.axivion.com/ Xref: g2news1.google.com comp.lang.ada:786 Date: 2008-06-20T19:12:37+02:00 List-Id: On Fri, 20 Jun, Reinert Korsnes wrote: > -- I want something more beautiful than this loop: > > for k in 1 .. Positive(X.Length) loop > do_something(X.Element(k)); > end loop; Sorry to high-jack this thread, but I'd like to comment on a somewhat related topic: For a very long time I was looking for some way to return a range from a function so that you can do something elegant like in the following: for I in Get_Range loop Do_Something (I); end loop; I always thought it to be a deficiency of Ada not being able to return a range from a function. A few weeks ago I realised the following solution, which I'd like to share: generic type Index_Type is (<>); package Generic_Range_Objects is type Empty_Component is null record; for Empty_Component'Size use 0; type Range_Type is array (Index_Type range <>) of Empty_Component; pragma Pack (Range_Type); end Generic_Range_Objects; And an instance for Integer ranges ... with Generic_Range_Objects; package Integer_Range_Objects is new Generic_Range_Objects (Integer); Now you can do the following: function Get_Range return Integer_Range_Objects.Range_Type is begin return (42 .. 128 => <>); end Get_Range; And then: for I in Get_Range'Range loop Do_Something (I); end loop; It's not as elegant as I wished (because of the additional 'Range), but it is simple and light-weight enough to be usable. -- Stefan Bellon