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-03 01:02:50 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!tar-atanamir.cbb-automation.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: discriminant in constraint must appear alone Date: Wed, 03 Dec 2003 10:06:41 +0100 Message-ID: References: NNTP-Posting-Host: tar-atanamir.cbb-automation.de (212.79.194.116) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1070442168 71316936 212.79.194.116 ([77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:3088 Date: 2003-12-03T10:06:41+01:00 List-Id: On Tue, 02 Dec 2003 21:15:36 GMT, tmoran@acm.org wrote: >> > 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; >> >Are there other possiblities? > >> type R (D1, D2 : Positive) is new Ada.Finalization.Controlled with > > If you use Controlled, then this could be one of those occasions where >access types are really useful. > > type String_Ptr_Type is access String; > procedure Free is new Ada.Unchecked_Conversion(String, String_Ptr_Type); > type R (D : Positive) is > record > A : String_Ptr_Type; > B : String_Ptr_Type; > end record; > >Then "R.A" needs to be "R.A.all", but R.A(i) can stay untouched. > > procedure Initialize (Obj : in out R) is > begin > A := new String(1 .. Obj.D); > B := new String(1 .. Obj.D/2); > end Initialize; > procedure Finalize (Obj : in out R) is > begin > Free(Obj.B); > Free(Obj.A); > end Finalize; Well, if heap allocation comes into play then better: type R is new Ada.Finalization.Controlled with private; function Create (D : Positive) return R; -- Constructor procedure Adjust (X : in out R); -- Copying constructor procedure Finalize (X : in out R); -- Destructor function A (X : R) return String; -- Getters function B (X : R) return String; ... private type R_Implementation (D1, D2 : Positive) is record A : String (1..D1); B : String (1..D2); end R_Implementation; type R_Implementation_Ptr is access R_Implementation; type R is new Ada.Finalization.Controlled with record Implementation : R_Implementation_Ptr; end record; Implemented like: function Create (D : Positive) return R is begin return ( Implementation => new R_Implementation (D, D / 2) ); end Create; procedure Adjust (X : in out R) is begin X.Implementation := new R_Implementation'(X.Implementation.all); end Adjust; procedure Finalize (X : in out R) is begin Free (X.Implementation); end Finalize; --------- But I think it is better to change the standard! (:-)) -- Regards, Dmitry Kazakov http://www.dmitry-kazakov.de