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,8276b2994037cd71 X-Google-Attributes: gid103376,public From: wayne lydecker Subject: Re: disjoint ranges ? Date: 2000/10/13 Message-ID: <39E79F17.24DB0828@pacbell.net>#1/1 X-Deja-AN: 681196837 Content-Transfer-Encoding: 7bit References: <39E612C9.9BF98CD3@laas.fr> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@cts.com X-Trace: thoth.cts.com 971480811 424 63.202.234.84 (13 Oct 2000 23:46:51 GMT) Organization: CTSnet Internet Services Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-10-13T00:00:00+00:00 List-Id: >Philippe Torres wrote: > >> A very dumb question but i can't find the right syntax so here i am. >> How can i declare a type or subtype with a disjoint range ? >> >> Example : >> >> If i declare : >> --- >> type Hours is new Integer range 0 .. 23 ; >> -- >> i would like a >> --- >> subtype Lunch_Hours is Hours range 7..9 & 12..14 & 19..21 ; >> --- > >The problem of disjoint ranges came up for me many years ago on a different >project. I >even suggested this as a capability for Ada 9X, but my suggestion either >fell through the >cracks or I submitted it incorrectly. > >My first attempt at a solution was to try to overload the "in" test as a >boolean function. That >turns out to be illegal for a variety of reasons that require careful study >of the language rules >for the "in" test. It turns out that membership tests are not >operations/operators in Ada. > >I finally ended up declaring a function that would do this test, but did >not use operator >overloading. It does solve the problem, but requires you call the >function each place >you intend to use a value of the type. > >A better approach would probably be to add a capability for assertion >checking. >This simple mechanism could be added at the type level or the subprogram >level >within the package specification. The notion of range checks was a good >one in >the early design of Ada. Perhaps the same thing can be accomplished >through >assertions in some future version. > >Richard Riehle How about using a boolean array: with Text_IO; procedure lunch is type Hours is new Integer range 0 .. 23 ; Lunch_Hours : array (Hours) of Boolean := (7..9 => True, 12..14 => True, 19..21 => True, others => False); begin if Lunch_Hours(7) then text_io.put_line("Lunch time"); else text_io.put_Line("Get back to work"); end if; end; -- Wayne.