comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org>
Subject: Re: subtype of enumeration type
Date: Mon, 20 Mar 2006 21:41:44 GMT
Date: 2006-03-20T21:41:44+00:00	[thread overview]
Message-ID: <skFTf.835639$x96.281917@attbi_s72> (raw)
In-Reply-To: <dvmas1$kj8$1@sunnews.cern.ch>

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 <what goes here?>;

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



  parent reply	other threads:[~2006-03-20 21:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-20 13:31 subtype of enumeration type Maciej Sobczak
2006-03-20 14:26 ` Larry Kilgallen
2006-03-20 15:18   ` Maciej Sobczak
2006-03-20 15:37 ` Robert A Duff
2006-03-20 20:29 ` Björn Persson
2006-03-20 21:41 ` Jeffrey R. Carter [this message]
2006-03-21  8:37   ` subtypes and preconditions (was: subtype of enumeration type) Peter Amey
2006-03-21 17:42     ` subtypes and preconditions Jeffrey R. Carter
2006-03-21 19:00       ` Georg Bauhaus
2006-03-22  1:54         ` Jeffrey R. Carter
replies disabled

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