comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Question about base types
Date: 1997/01/30
Date: 1997-01-30T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280003001970926560001@news.ni.net> (raw)
In-Reply-To: mheaney-ya023280003001970055250001@news.ni.net


In article <mheaney-ya023280003001970055250001@news.ni.net>, mheaney@ni.net
(Matthew Heaney) wrote:


>Here's what I want to do.  Suppose I have a floating point heading type,
>and I want to override its primitive add operator so that it behaves like a
>modulo number, as you'd expect headings to do:
>
>type Heading is digits 6 range 0.0 .. 360.0;
>
>function "+" (L, R : Heading) return Heading;
>
>function "-" (L, R : Heading) return Heading;
>
>So that 
>
>H : Heading := 359.0 + 2.0;   -- H has the value 1.0
>
>H : Heading := 1.0 - 2.0;  -- H has the value 359.0
>
>Can I do this implementation below, legally and portably?
>
>function "+" (L, R : Heading) return Heading is
>   Sum : constant Heading'Base := L + R;
>begin
>   if Sum > Heading'Last then
>      return Sum - Heading'Last;
>   else 
>      return Sum;
>   end if;
>end;

I think I've got it!

My type declarations should have been

type Heading_Base is digits 6 range 0.0 .. 720.0;
subtype Heading is Heading_Base range 0.0 .. 360.0;

function "+" (L, R : Heading) return Heading is
   Sum : Heading'Base := L + R;   -- Sum has the range 0.0 .. 720.0

Just a quick little question: the "+" operator is a primitive operation of
Heading_Base, right?  Can subtypes that are not first subtypes have
primitive operations (different from their parent's), or only first
subtypes?  (Or should I have said  ... only types?)

>generic
>   type T is digits <>;
>function Generic_Heading_Add (L, R : T) return T;
>
>function Generic_Heading_Add (L, R : T) return T is
>   Sum : constant T'Base := L + R;
>begin
>   if Sum > T'Last then
>      return Sum - T'Last;
>   else
>      return Sum;
>   end if;
>end;

So this will work if I instantiate it on type Heading, because then T'Base
will have the range I need.  So it's up to the instantiator to make sure
that T'Base has the required range.  And of course the spec of the generic
should have a comment or something about what the required range of T'Base
is.

You know, it would be really swell if the language had a way of formally
stating in the spec (rather than as comments) what the range of T'Base
needs to be.  Sound like another language you've heard of?  Une langue qui
parle francias?

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-01-30  0:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-01-26  0:00 Question about base types Matthew Heaney
1997-01-27  0:00 ` Robert A Duff
1997-01-30  0:00   ` Matthew Heaney
1997-01-30  0:00     ` Matthew Heaney [this message]
1997-01-30  0:00     ` Robert Dewar
1997-01-30  0:00       ` Matthew Heaney
     [not found]     ` <EACHUS.97Feb3221558@spectre.mitre.org>
     [not found]       ` <dewar.855063927@merv>
     [not found]         ` <EACHUS.97Feb6145918@spectre.mitre.org>
     [not found]           ` <dewar.855276722@merv>
1997-02-07  0:00             ` Mats Weber
1997-02-08  0:00               ` Robert Dewar
1997-02-10  0:00                 ` Mats Weber
1997-02-11  0:00                   ` Robert Dewar
     [not found]         ` <32FB45D4.2160@watson.ibm.com>
1997-02-10  0:00           ` Robert Dewar
1997-02-08  0:00   ` Robert Dewar
1997-02-09  0:00     ` Matthew Heaney
1997-02-09  0:00       ` Robert Dewar
1997-02-09  0:00         ` Matthew Heaney
1997-02-10  0:00           ` Robert Dewar
1997-02-10  0:00       ` Larry Kilgallen
1997-02-11  0:00   ` Robert I. Eachus
1997-02-12  0:00     ` Robert Dewar
1997-01-27  0:00 ` Mats Weber
1997-01-28  0:00   ` Robert A Duff
1997-01-29  0:00   ` Robert Dewar
1997-01-28  0:00 ` Robert I. Eachus
1997-01-28  0:00   ` Mats Weber
1997-01-29  0:00 ` Robert I. Eachus
1997-01-30  0:00   ` Robert A Duff
replies disabled

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