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!news4.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Why can't you create a out of order subtype? Date: 03 Feb 2005 18:09:05 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1107301914.648240.237290@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: shell01-e.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1107472145 2881 69.38.147.31 (3 Feb 2005 23:09:05 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 3 Feb 2005 23:09:05 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:8158 Date: 2005-02-03T18:09:05-05:00 List-Id: brett_gengler@yahoo.com writes: > 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? Because Jean Ichbiah said so. ;-) > Is it true that I can't define a a subtype that's not a ordered > subrange of a type? It's a reasonable thing to want, but Ada does not support it. >...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. But subtypes in Ada are checked at run time. So if the language supported what you want, it would still be a run-time check. An implicit run-time check, as opposed to the explicit code you mention below. It has to be a run-time check, in the general case. There are some cases where these things can be checked at compile time, and some compilers give warnings in those cases. But compilers vary in how smart they are about warnings, and the absence of warnings does not imply an absence of errors in the code. >... (This is for an interface and I don't trust people to send > me the right types). > > vowel_filter is array(msg) of Boolean := (msg_a => True, msg_e => True, > others => False); --yuck. You can say (msg_a | msg_e => True, others => False), which is a little bit more concise. You can also decide whether to Pack the array. But "others" is usually evil. If you add "msg_i" later, it will be picked up by that others. But if you spell out "msg_b|msg_c|msg_d" now, then when you add msg_i, you'll get a compile-time error reminding you to decide whether or not it's a vowel. > 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. I agree -- that's not a very good solution. > Am I screwed? Should I just implement a boolean array and shut up > about it? You should probably implement the boolean array. But you don't have to shut up about it. ;-) In this particular case, you can do: subtype Consonant_Msg is Msg range msg_b..msg_d; if blah not in Consonant_Msg then ... but there's no general way to get arbitrary subtypes of an enumeration type. - Bob