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,e58bb9b46b60f0fb X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!attbi_s72.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" Organization: jrcarter at acm dot org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: subtype of enumeration type References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 12.214.35.215 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s72 1142890904 12.214.35.215 (Mon, 20 Mar 2006 21:41:44 GMT) NNTP-Posting-Date: Mon, 20 Mar 2006 21:41:44 GMT Date: Mon, 20 Mar 2006 21:41:44 GMT Xref: g2news1.google.com comp.lang.ada:3506 Date: 2006-03-20T21:41:44+00:00 List-Id: Maciej Sobczak wrote: > > type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); > > and the following standard example: > > subtype Weekday is Day range Mon .. Fri; > > Now imagine that in some country children don't have to go to school on > Wednesdays, they go there only Monday, Tuesday, Thursday and Friday. > > I want to say it in Ada. > > subtype Schoolday is Day ; Nothing. You can't do it that way in Ada. There are a number of options: 1. Use subtype Schoolday is Weekday; add a comment that Wed is invalid, check for Wed and raise an exception. Not very nice, but adheres to the practice of specifying preconditions and raising exceptions when they're violated. However, using subtypes is cleaner. I much prefer function F (A : Natural); to function F (A : Integer); --# pre A >= 0; which is often seen in SPARK. 2. Use a private type: type Schoolday is private; -- Mon, Tue, Thu, Fri function Is_Schoolday (Today : Weekday) return Boolean; -- returns False if Today = Wed; True otherwise function "+" (Right : Weekday) return Schoolday; -- pre Right /= Wed; raise Constraint_Error if violated function "+" (Right : Schoolday) return Weekday; procedure Op (Today : in Schoolday); This is similar to the previous example, but the checking only needs to be on the conversion function. 3. Use sets of Day; Schoolday is a set with Mon, Tue, Thu, and Fri as members. 4. Use 2 subtypes: subtype Early_Schoolday is Day range (Mon .. Tue); subtype Late_Schoolday is Day range (Thu .. Fri); What you do will depend on the requirements of your application. In general, there is no way to specify Day that will allow subtypes for all possible uses of days of the week. -- Jeff Carter "From this day on, the official language of San Marcos will be Swedish." Bananas 28