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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,d00514eb0749375b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: Re: initialize an array (1-D) at elaboration using an expression based on the index? Date: Fri, 15 Oct 2010 17:29:46 -0700 Organization: Aioe.org NNTP Server Message-ID: References: <4b8f7f06-a817-4545-9fc6-67740c67b9d3@a4g2000prm.googlegroups.com> Reply-To: nma@12000.org NNTP-Posting-Host: tUYQ4Ty9mMw9Pdc8TJRFQA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100915 Thunderbird/3.1.4 Xref: g2news2.google.com comp.lang.ada:15525 Date: 2010-10-15T17:29:46-07:00 List-Id: On 10/15/2010 5:16 PM, Adam Beneschan wrote: > On Oct 15, 4:31 pm, "Vinzent Hoefler" > It seems like it would be simple to add syntax to array aggregates in > the language like > > a : array(1..N) of Float := (for I in 1..N => Float(I)*Float(I)); > > The legality rules and rules about applicable index constraints would > be the same as if "for I in" were not present. > > Perhaps someone has already made a similar proposal, but I can't find > it. Since Ada 2012 is already putting "if" and "case" in expressions, > though, it shouldn't be too big a leap to include "for" in an > expression. Of course, it's too late to make it into Ada 2012, but > perhaps I'll propose it for the next version of Ada (what is that > going to be? Ada 2020, where we add all the features that we realize > in hindsight that we should have added earlier? :-)). > > -- Adam > That would be neat if this can be done. Because many times one needs to init an array or a matrix to some initial values, it is a bit more concise if one can do it in the 'same place' but Mr Robert Duff send me a nice solution to use, which is to use an init() function. Here is my code now using his idea. So, using his solution, it is almost as good as doing the initialization 'in place'. ----------------------------------- -- gnatmake main.adb -- platform: gcc 4.5 with Ada support. -- with ada.float_text_io; use ada.float_text_io; with ada.text_io; use ada.text_io; procedure main is initial_h : constant float := 1.0/8.0; nIter : constant integer := 6; type h_type is array(1..nIter) of float; -- INIT function to use to initialize array h below function Init_h return h_type is -- NEED THIS HERE begin return result : h_type do for i in Result'Range loop Result(i) := 1.0/(2.0**i); end loop; end return; end Init_h; h : constant h_type := init_h; -- INIT the array here begin for i in h'range loop put(h(i)); new_line; end loop; end main; ------------------------------------- $ gnatmake main.adb $ ./main.exe 5.00000E-01 2.50000E-01 1.25000E-01 6.25000E-02 3.12500E-02 1.56250E-02 --Nasser