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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2e8cf506f89b5d0a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-20 07:02:20 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!213.56.195.71!fr.usenet-edu.net!usenet-edu.net!enst!enst.fr!not-for-mail From: "M. A. Alves" Newsgroups: comp.lang.ada Subject: Re: Looping over a tagged record? Date: Wed, 20 Jun 2001 16:01:28 +0100 (WEST) Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: avanie.enst.fr 993045737 64893 137.194.161.2 (20 Jun 2001 14:02:17 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Wed, 20 Jun 2001 14:02:17 +0000 (UTC) To: comp.lang.ada@ada.eu.org Return-Path: X-Sender: maa@borba.ncc.up.pt In-Reply-To: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.4 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:8919 Date: 2001-06-20T16:01:28+01:00 > >. . , I was wondering if there is anyway to loop over all elements in > >the record, checking their type and then taking the right action... > > If you really need to do this, you probably should have put the > bounded strings into an array inside the record, instead of just > making them flat fields. Often I also want to merge record and array i.e. have named _and_ iteratable "components". Theoretically this comes down to "types as objects", which Ada doesn't have. Practically, a "standard" Ada idiom for this is an heterogenous array indexed on an enumeration e.g. (not tested): ... type Field_Ptr is access Field'Class; type Field_Names is (Field_1, Field_2, Field_3); type Record_Type is array(Field_Names) of Field_Ptr; A_Record: Record_Type := ( Field_1 => Field_Constructor_A(...), Field_2 => Field_Constructor_A(...), Field_3 => Field_Constructor_B(...)); Now we have a record-like object A_Record with "components" Field_1, Field_2 of "type" A and Field_3 of "type" B. This example assumes class-wide programming (including dispatching), with (tagged) type Field being the root of some derivation class. Iterating over A_Record's "components": for I in A_Record'Range loop Do_Something(A_Record(I).all); -- dispatching call end loop; Accessing "components" by name: Do_Something(A_Record(Field_1).all); This particular approach has a notorious shortcoming: field types are not checked (Field_Ptr values can reference _any_ derived type in the class). The remedy for this is left as an exercise ;-) -- , M A R I O data miner, LIACC, room 221 tel 351+226078830, ext 121 A M A D O Rua Campo Alegre, 823 fax 351+226003654 A L V E S P-4150 PORTO, Portugal mob 351+939354002