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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f20f5dfbb5c26c12 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-02 12:57:45 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: discriminant in constraint must appear alone Date: Tue, 2 Dec 2003 14:56:18 -0600 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-Complaints-To: abuse@supernews.com Xref: archiver1.google.com comp.lang.ada:3077 Date: 2003-12-02T14:56:18-06:00 List-Id: "Vincent Smeets" wrote in message news:bqiq1m$uis$00$1@news.t-online.com... > Hallo, > > I want to define a discriminated record type with two arrays. One array only half the size > of the other. Below is the type definition that I want to use. > > type R (D : Positive) is > record > A : String (1 .. D); > B : String (1 .. D / 2); > end record; > > This type definition can't be compiled by the GNAT compiler. It gives the error message > "discriminant in constraint must appear alone" >for the record component B. I know this isn't correct Ada, but how should I define the type >in a correct way? I don't think you can. >I have defined B as > B : String (1 .. D); >and only used the first half of it, but this way I waist memory and can't have any Ada >constraint checks for component B. So this is not the way I want to do it. > >Are there other possiblities? The only thing that comes to mind is to have a second discriminant. The problem with that is that the relationship between them can't be defined formally. type R (D1, D2 : Positive) is record A : String (1 .. D1); B : String (1 .. D2); end record; Obj : R (Max_Size, Max_Size/2); I suppose you could have Initialize check if they don't match and raise an exception: type R (D1, D2 : Positive) is new Ada.Finalization.Controlled with record A : String (1 .. D1); B : String (1 .. D2); end record; procedure Initialize (Obj : in out R); procedure Initialize (Obj : in out R) is begin if Obj.D1 /= Obj.D2*2 then raise Constraint_Error; -- Or better Ada.Exceptions.Raise_with_Message (Constraint_Error'Identity, "D2 is not half of D1"); end if; end Initialize; Obj : R (Max_Size, Max_Size/2); Obj2 : R (Max_Size, Max_Size); -- Would raise Constraint_Error. But that seems like a lot of mechanism for a simple check. (OTOH, my theory is that virtually all types ought to be derived from Controlled or Limited_Controlled anyway, so this doesn't add much in that case.) Randy.