comp.lang.ada
 help / color / mirror / Atom feed
From: olivermkellogg@gmail.com
Subject: Re: Generic formals and Aspects
Date: Tue, 19 Jul 2016 08:49:30 -0700 (PDT)
Date: 2016-07-19T08:49:30-07:00	[thread overview]
Message-ID: <59656e9a-8826-490e-9c35-f13f8ff1aa91@googlegroups.com> (raw)
In-Reply-To: <lya94ultsg.fsf@pushface.org>

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;


  parent reply	other threads:[~2016-07-19 15:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-17 13:17 Generic formals and Aspects Simon Wright
2014-10-17 16:04 ` Shark8
2014-10-17 18:51   ` Simon Wright
2014-10-18  1:43 ` Shark8
2016-07-19 15:49 ` olivermkellogg [this message]
2016-07-19 16:04   ` J-P. Rosen
2016-07-19 16:05   ` Shark8
2016-07-19 18:00     ` olivermkellogg
2016-07-19 19:23       ` Randy Brukardt
2016-07-20 15:09         ` olivermkellogg
replies disabled

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