comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Type Transcriptions
Date: Mon, 21 Apr 2014 22:33:47 +0200
Date: 2014-04-21T22:33:47+02:00	[thread overview]
Message-ID: <87ha5m1kyc.fsf@ludovic-brenta.org> (raw)
In-Reply-To: WMd5v.117886$vS2.93071@fx32.iad

Shark8 <OneWingedShark@gmail.com> writes:
> On 21-Apr-14 12:11, Yannick Duchêne (Hibou57) wrote:
>> Le Mon, 21 Apr 2014 18:54:23 +0200, Brad Moore <brad.moore@shaw.ca> a
>> écrit:
>>
>>> I'd suggest trying to make GenericAEADCipher a discriminated record
>>> type, with the two length values as the discriminants, and then the
>>> arrays are sized using the discriminants. No Generics needed.
>>>
>>> Brad
>>>
>>
>> I guess Shark8 would already have figured it if this could be as simple.
>> He says this, which is important: “certain class of record-type is
>> dependent on the values in another record, yet distinctly separate”.
>> With two separated things, a discriminant is not applicable. Or may be
>> I've not understood.
>
> You have the right of it.
> Using discriminants is the natural way to do it, but doing so would
> (in actuality) break sticking to the spec as the record-type would
> have fields which don't exist in the spec.
>
> I suppose the better way to phrase it is:
> "Is it better to strictly stick to the spec, letting generic
> instantiation take care of these 'extra-typal' parameters, *OR* doing
> things the natural Ada way, and worrying about 'fixing-up' things in,
> say, 'Input and 'Output operations."

Ada 2012 has a very nice new feature: unchecked unions (clause B.3.3);
it is a discriminated record with an aspect says that the discriminants
are *not* in the record, but stored elsewhere instead.  I have used them
in my tiny Ada binding to the X C binding (XCB), like so:

      type Detail_T (Response_Type : Response_Type_T := Reply) is record
         case Response_Type is
            when Key_Press | Key_Release =>
               Keycode : Interfaces.Unsigned_8;
            when Button_Press | Button_Release | Motion_Notify =>
               Detail  : Button_T;
            when others =>
               Padding : Interfaces.Unsigned_8;
         end case;
      end record
         with Unchecked_Union;

      type Response_T (Sent : Boolean; Response_Type : Response_Type_T) is
         record
            Detail   : Detail_T (Response_Type);
            [other components omitted]
         end record;
      for Response_T use record
         Sent          at 0 range 7 .. 7;
         Response_Type at 0 range 0 .. 6;
      end record;


This says that the Response_T has two discriminants, stored as the first
two components, and that the second discriminant, Response_Type, also
constrains the component Detail, but Detail does not contain a copy of
this discriminant.

This sounds awfully similar to the spec you're trying to implement :)

In this case, Ada 2012 saves the day!

-- 
Ludovic Brenta.

  parent reply	other threads:[~2014-04-21 20:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-21  7:21 Type Transcriptions Shark8
2014-04-21 16:54 ` Brad Moore
2014-04-21 18:11   ` Yannick Duchêne (Hibou57)
2014-04-21 18:51     ` Shark8
2014-04-21 19:42       ` Jeffrey Carter
2014-04-22  6:06         ` Shark8
2014-04-21 20:33       ` Ludovic Brenta [this message]
2014-04-21 22:46         ` Adam Beneschan
2014-04-22  8:57         ` Shark8
replies disabled

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