comp.lang.ada
 help / color / mirror / Atom feed
From: "Mark A. Biggar" <mark@biggar.org>
Subject: Re: Overlapping ranges
Date: Sun, 22 Jun 2003 01:23:56 GMT
Date: 2003-06-22T01:23:56+00:00	[thread overview]
Message-ID: <My7Ja.54206$hz1.129990@sccrnsc01> (raw)
In-Reply-To: <EU6Ja.43611$JA5.766429@news.xtra.co.nz>

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!"

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

First option fix the abstraction:

package range is
    type xBase is range 0..2**15-1;
    subtype x1 is xBase range 0..255;
    subtype x2 is xBase range 127..2**15-1;
    subtype x1x2Intersect is xbase range 127..255;

    function foo(p: x1x2Intersect) return ...;
  end ranges;

If you really need two separate types:

package range is
    type xBase is range 0...2**15-1;
    subtype x1x2Intersect is xBase range 127..255;
    type x1 is new xBase range xBase'First..255;
    type x2 is new xBase range 127..xBase'Last;

    function foo(p: x1x2intersect) return ...;
end ranges;

But now you must call foo with a view conversion to xBase:
a := foo(xBase(200));

Another option is to use a discriminated record:

package range is
    type x1 is range 0..255;
    type x2 is range 127..2**15-1;

    type x1x2intersect(b: boolean) is record
	case b is
	when false => x1val: x1 range 127..255;
	when true  => x2val: x2 range 127..255;
	end case;
    end record;

    function foo(p: x1x2intersect) return ...;
end ranges;

But now you need to call foo like:
a := foo((false, x1'(200)));
or
a := foo((true,  x2'(200)));


Personally, I'd try to get the design fixed :-)

-- 
mark@biggar.org
mark.a.biggar@attbi.com




  reply	other threads:[~2003-06-22  1:23 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 [this message]
2003-06-22  2:58   ` AG
2003-06-22  3:17     ` tmoran
2003-06-22 17:55   ` Robert I. Eachus
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