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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d57302f2954365e1 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Question about base types Date: 1997/01/30 Message-ID: #1/1 X-Deja-AN: 213277502 references: content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1997-01-30T00:00:00+00:00 List-Id: In article , 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 (818) 985-1271