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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,115cdbb394b3e615 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Why can't you create a out of order subtype? References: <1107301914.648240.237290@g14g2000cwa.googlegroups.com> In-Reply-To: <1107301914.648240.237290@g14g2000cwa.googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Wed, 02 Feb 2005 00:24:45 GMT NNTP-Posting-Host: 63.186.49.133 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.pas.earthlink.net 1107303885 63.186.49.133 (Tue, 01 Feb 2005 16:24:45 PST) NNTP-Posting-Date: Tue, 01 Feb 2005 16:24:45 PST Xref: g2news1.google.com comp.lang.ada:8125 Date: 2005-02-02T00:24:45+00:00 List-Id: brett_gengler@yahoo.com wrote: > I created a type whose order can not be rearranged and I want to create > a subtype of that type. So, > > type msg is ( msg_a, msg_b, msg_c, msg_d, msg_e); > > subtype vowel_msg is msg (msg_a, msg_e); --wrong, but why? Ada only allows subrange subtypes of enumeration types. Sure, there are a lot of things it would be nice to be able to do, but the language only allows certain things. > Is it true that I can't define a a subtype that's not a ordered > subrange of a type? My issue is that I want to enforce the subtype at > compile time instead of creating an array of booleans and doing a run > time check. (This is for an interface and I don't trust people to send > me the right types). It's true for elementary types. > Another option that I think is much, much worse then the boolean array > would be create an overloaded type... > > type msg is ( msg_a, msg_b, msg_c, msg_d, msg_e); > type vowelmsg ( msg_a, msg_e); > > But as you know, msg.msg_e = 4 and vowelmsg.msg_e = 1 so an unchecked > conversion is wrong on so many levels. Yes, this may be worse, but you can at least get Unchecked_Conversion to work: for Vowelmsg use (Msg_A => 0, Msg_E => 4); for Vowelmsg'Size use Msg'Size; This would still be a run-time check: function To_Vowel is new Ada.Unchecked_Conversion (Source => Msg, Target => Vowelmsg); It_Is_A_Vowel : constant Vowelmsg := To_Vowel (Any_Msg); if It_Is_A_Vowel'Valid then -- It's a vowel! -- Jeff Carter "You cheesy lot of second-hand electric donkey-bottom biters." Monty Python & the Holy Grail 14