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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2adf79f4e850a932 X-Google-Attributes: gid103376,public From: stt@houdini.camb.inmet.com (Tucker Taft) Subject: Re: Curious Question Date: 1997/12/05 Message-ID: #1/1 X-Deja-AN: 295509013 Sender: news@inmet.camb.inmet.com (USENET news) X-Nntp-Posting-Host: houdini.camb.inmet.com References: <66757i$mk5$1@swlab1.msd.ray.com> Organization: Intermetrics, Inc. Newsgroups: comp.lang.ada Date: 1997-12-05T00:00:00+00:00 List-Id: Chad Reichenbach {81726} (car@swl.msd.ray.com) wrote: : I have a question I can'f figure out. If I have the following code: : type My_Type is (obj1, obj2 obj3, obj4, obj5, obj6); : subtype My_Subtype is My_Type range obj1 .. obj4; : My question is, I need a new subtype that ranges from obj4 to obj1. : Will the declaration : subtype Second_Subtype is My_Subtype : range My_Subtype'last .. My_Subtype'first : do that? No, this would declare a subtype with a null range, having no values at all. : ... If not is there a way to do it without defining a new : enumeration? You can iterate backwards through a range via: for I in reverse My_Subtype loop But it sounds like you might want to iterate around in a circle, going obj4, obj5, obj6, obj1. Is that right? If so, then you might want to declare a modular type, which has wrap-around semantics for S'Succ: type Wrap_Around is mod My_Type'Pos(obj6)+1; and then you could iterate as follows: declare I : Wrap_Around := My_Type'Pos(Obj4); begin loop declare J : My_Type := My_Type'Val(I); begin -- use "J" as necessary exit when J = Obj1; end; I := Wrap_Around'Succ(I); end loop; end; Note that a "for" loop over the Wrap_Around would still not work, because a for loop is not executed at all if the starting value is greater than the ending value, even for a modular type. So this would *not* work as desired: for I in Wrap_Around range My_Type'Pos(Obj4) .. My_Type'Pos(Obj1) loop -- Will never get here end loop Of course, simpler than all the above would be to simply write a loop as follows: declare J : My_Type := Obj4; begin loop -- use "J" as necessary exit when J = Obj1; if J = Obj6 then J := Obj1; else J := My_Type'Succ(J); end if; end loop; end; : The type would be used in a loop that loops through the enumeration : objects using S'succ(S). : TIA, : chad -- -Tucker Taft stt@inmet.com http://www.inmet.com/~stt/ Intermetrics, Inc. Burlington, MA USA