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,fcf8ea94b94d6941 X-Google-Attributes: gid103376,public From: Ole-Hjalmar Kristensen Subject: Re: Modular type. What is it and why? Date: 1999/04/06 Message-ID: #1/1 X-Deja-AN: 463323348 X-NNTP-Posting-Host: fwall.clustra.com References: <7ean3c$79m$1@eol.dd.chalmers.se> <7eb2iq$jc3@hobbes.crc.com> <7ede2p$pd1$1@its.hooked.net> Newsgroups: comp.lang.ada X-Complaints-To: abuse@telia.no Date: 1999-04-06T00:00:00+00:00 List-Id: "Mike Silva" writes: > David C. Hoos, Sr. wrote in message <7eb2iq$jc3@hobbes.crc.com>... > <....> > >There are many uses for such a type -- e.g., the index of a circular array, > or a representation of the days of the week... > > One of the little "goodies" I discovered in coming from C to Ada are modular > types. I often had to write code like: > > switch ( key ) > { > case ARROW_DOWN: > if ( index < ( NUM_ELEMENTS - 1 ) ) > index++; > else > index = 0; > break; > case ARROW_UP: > if ( index > 0 ) > index = NUM_ELEMENTS - 1; > else > index--; > break; > } > > Now with Ada I'll be able to write (assuming 'index' is the appropriate > modular type): > > case ( key ) is > when ARROW_DOWN => > index := index + 1; > when ARROW_UP => > index := index - 1; > end case; > > Now -that's- what I call progress! > > Mike > > > > Yes, this is a good illustration of modular types. However, what do you do if NUM_ELEMENTS change? Maybe modular types isn't the right choice in this case. -- E pluribus Unix