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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.157.2.37 with SMTP id 34mr31381422otb.2.1468943370415; Tue, 19 Jul 2016 08:49:30 -0700 (PDT) X-Received: by 10.157.32.22 with SMTP id n22mr1817910ota.2.1468943370384; Tue, 19 Jul 2016 08:49:30 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!f6no2646307ith.0!news-out.google.com!d130ni7995ith.0!nntp.google.com!f6no2642416ith.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 19 Jul 2016 08:49:30 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=217.248.72.206; posting-account=Md_OIgoAAAAkZyQ6nYoc3WBIThMpPfV7 NNTP-Posting-Host: 217.248.72.206 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <59656e9a-8826-490e-9c35-f13f8ff1aa91@googlegroups.com> Subject: Re: Generic formals and Aspects From: olivermkellogg@gmail.com Injection-Date: Tue, 19 Jul 2016 15:49:30 +0000 Content-Type: text/plain; charset=UTF-8 Xref: news.eternal-september.org comp.lang.ada:31098 Date: 2016-07-19T08:49:30-07:00 List-Id: On Friday, October 17, 2014 at 3:17:36 PM UTC+2, Simon Wright wrote: > Recently on StackOverflow there was a question[1] about clamping a value > to a range. > > The answer, so far, suggests a generic: > > generic > type Source_Type is range <>; > type Destination_Type is range <>; > function Saturate (X : Source_Type) return Destination_Type; > > with discussion about what happens if Destination_Type'Range is not a > subset of Source_Type'Range. > > I see in AARM 13.1.1(4.b)[2] that a formal_type_declaration is allowed to > include an aspect specification, so tried > > generic > type Source_Type is range <> > with Static_Predicate => > Long_Long_Integer (Destination_Type'First) > >= Long_Long_Integer (Source_Type'First) > and Long_Long_Integer (Destination_Type'Last) > <= Long_Long_Integer (Source_Type'Last); > type Destination_Type is range <>; > function Saturate (X : Source_Type) return Destination_Type; > > but GNAT (4.9.1, GPL 2014) said that Static_Predicate wasn't allowed > (nor was Dynamic_Predicate). > > Predicate (GNAT-special?) was allowed, but had no effect: I was able to > instantiate with > > type Source is new Integer range 10 .. 20; > type Destination is new Integer range 30 .. 40; > > > (a) what aspects are/should be allowed in a formal_type_declaration? > > (b) how to write the generic to prevent this sort of mistake at compile > time? (easy enough to get a runtime CE). > > [1] http://stackoverflow.com/questions/26390135/can-i-clamp-a-value-into-a-range-in-ada > [2] http://www.ada-auth.org/standards/12aarm/html/AA-13-1-1.html#p4.b I share the sentiment. Here is my use case: -- File: big_endian_integer_buffer.ads with Interfaces; generic type Discrete_Type_16_or_32_or_64 is (<>); -- CANDIDATE package Big_Endian_Integer_Buffer is function Get return Discrete_Type_16_or_32_or_64; procedure Set (Value : Discrete_Type_16_or_32_or_64); Size_In_Bytes : constant Positive := Discrete_Type_16_or_32_or_64'Size / 8; type Buffer_Type is array (1 .. Size_In_Bytes) of Interfaces.Unsigned_8; for Buffer_Type'Component_Size use 8; Buffer : aliased Buffer_Type := (others => 0); end Big_Endian_Integer_Buffer; At the line marked CANDIDATE, I would have liked to write something like type Discrete_Type_16_or_32_or_64 is (<>) with Static_Predicate => Discrete_Type_16_or_32_or_64'Size in 16 | 32 | 64;