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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,bdd718faa7c79f29 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-22 10:55:49 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc02.POSTED!not-for-mail Message-ID: <3EF5EDA1.1010702@attbi.com> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Overlapping ranges References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.62.164.137 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc02 1056304548 24.62.164.137 (Sun, 22 Jun 2003 17:55:48 GMT) NNTP-Posting-Date: Sun, 22 Jun 2003 17:55:48 GMT Organization: AT&T Broadband Date: Sun, 22 Jun 2003 17:55:48 GMT Xref: archiver1.google.com comp.lang.ada:39570 Date: 2003-06-22T17:55:48+00:00 List-Id: 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...