comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: Overlapping ranges
Date: Sun, 22 Jun 2003 17:55:48 GMT
Date: 2003-06-22T17:55:48+00:00	[thread overview]
Message-ID: <3EF5EDA1.1010702@attbi.com> (raw)
In-Reply-To: My7Ja.54206$hz1.129990@sccrnsc01

Mark A. Biggar wrote:
> AG wrote:
> 
>> Assuming I have a package declaration like this:
>>
>>
>> package range is
>>   type x1 is range 0..255;
>>   type x2 is range 127..2**15-1;
>> end ranges;
>>
>>
>> Is there a way to declare a function
>> using that package that will accept
>> values of either x1 or x2 (and nothing
>> else) restricted to the range 127..255 ?
> 
> 
> You have a bug in your design abstraction.  x1 and x2 are different
> types and there is no way to declare a parameter with more then one
> type.  This is like the doctor joke: "patient: Doc it hurts when I do 
> this.  Doctor: So don't do that!"

That is the most important part of this advice.  Something is almost 
certainly wrong in your design, fix it.

> But if that's really what you want to do this, there are several way to
> do it.

And here is another.  (Again, this looks like a kludge because it is.)

Lower: X2 := X2'Max(X2'First, X2'Base(X1'First));
Upper: X2 := X2'Min(X2'Last, X2'Base(X1'Last));
subtype Restricted1 is X1 range X1(Upper)..X1(Lower);
subtype Restricted2 is X2 range Upper..Lower;

function Do_Something(Input: Restricted1) return ...;
function Do_Something(Input: Restricted2) return ...;

function Do_Something(Input: Restricted1) return ... is
begin return Do_Something(X2(Input)); end Do_Something;

function Do_Something(Input: Restricted2) return ... is
-- the "real" function...
...

Now let's see which problem requirements seem silly:

Why are X1 and X2 different types rather than subtypes?  Even if you 
need to crowd the X1 value into a byte in some record, it could be done 
with a subtype.

Why is X1 not a modular type?

Why do you need the Constraint_Error to be raised outside the function 
call?  Wouldn't raising it inside OR outside work just fine?

Why do you want one function which accepts parameters of two different 
types?  My solution has the call to one calling the other, but still...







  parent reply	other threads:[~2003-06-22 17:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-22  0:40 Overlapping ranges AG
2003-06-22  1:23 ` Mark A. Biggar
2003-06-22  2:58   ` AG
2003-06-22  3:17     ` tmoran
2003-06-22 17:55   ` Robert I. Eachus [this message]
2003-06-23  9:05     ` AG
2003-06-23 10:46       ` David C. Hoos
2003-06-24  8:09         ` AG
2003-06-29  0:36 ` Richard Riehle
2003-06-29  6:24   ` AG
2003-06-29  6:42     ` AG
2003-06-29 18:40       ` Richard Riehle
2003-06-29 18:49     ` Richard Riehle
replies disabled

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