comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Why can't you create a out of order subtype?
Date: 03 Feb 2005 18:09:05 -0500
Date: 2005-02-03T18:09:05-05:00	[thread overview]
Message-ID: <wcclla5utpq.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 1107301914.648240.237290@g14g2000cwa.googlegroups.com

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



      parent reply	other threads:[~2005-02-03 23:09 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-01 23:51 Why can't you create a out of order subtype? brett_gengler
2005-02-02  0:24 ` Jeffrey Carter
2005-02-02 17:55   ` Marius Amado Alves
2005-02-02 18:50     ` Pascal Obry
2005-02-02 20:22       ` Marius Amado Alves
2005-02-03  4:36       ` Wes Groleau
2005-02-03 12:59         ` Marius Amado Alves
2005-02-04  3:42           ` Wes Groleau
2005-02-02 19:35     ` Martin Dowie
2005-02-02 20:35       ` Marius Amado Alves
2005-02-02 21:18         ` Martin Dowie
2005-02-03 19:40       ` Robert A Duff
2005-02-03 20:22         ` Marius Amado Alves
2005-02-03 22:05           ` Robert A Duff
2005-02-04  6:49           ` Martin Dowie
2005-02-02 22:26 ` Georg Bauhaus
2005-02-03 23:09 ` Robert A Duff [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox