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=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,4ce5890331a5b529 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: Discriminants of tagged types Date: Thu, 28 Oct 2010 00:07:05 +0200 Organization: Ada @ Home Message-ID: References: <14314714-e92c-4036-9cbb-da8e72489261@h7g2000yqn.googlegroups.com> NNTP-Posting-Host: rx94L06a5qReYJRQTebs6g.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 User-Agent: Opera Mail/10.63 (Win32) Xref: g2news1.google.com comp.lang.ada:14871 Date: 2010-10-28T00:07:05+02:00 List-Id: I see your good point. I have something similar for Windows API interfacing (ex. for event = handlers whose parameters may be made of two short integer, a reference,= a = long integer, and so, in the same parameter, depending on a message ID).= = This is a particular case, because I use Unchecked_Union, and = Unchecked_Union requires a default value for the discriminant. Otherwise, in your example, I would say what you named Value, is not a = Value, and rather an Expression, unless you meant an abstract view of wh= at = a Value is. As there are Integer_Value, Float_Value, String_Value, I suppose each of= = these three is associated to specific operations. So why not three types= ? = Tagged types derived from a root abstract tagged type ? And for memory management, a pool of tagged type : a pool of = Integer_Value, a pool of Float_Value, and so on. You will even save memo= ry = this way. About saving memory, image you have thousands of = ASCII_Character_Value, each of them allocating all the space which would= = be required to allocate a Float_Value, that is something between 32 and = 64 = bits. But that is easily said, as I do not know about the whole system where = this excerpt could come from. Still not convinced. Le Wed, 27 Oct 2010 22:26:57 +0200, Adam Beneschan a = =C3=A9crit: > I think that *modifiable* discriminants (which means default > discriminants in Ada syntax) would be hard to do without, in variant > record cases. Suppose, for instance, you were using Ada to write an > interpreter for Perl or some other language that uses variables whose > type could change between an integer, a floating-point value, or a > string. It seems natural to define a variable's value as a variant > record: > > type Value (Kind : Value_Kinds :=3D None) is record > case Kind is > when Integer_Value =3D> Int : Long_Integer; -- or your > choice of integer type > when Float_Value =3D> Flt : Float; -- ditto > when String_Value =3D> Str : > Ada.Strings.Unbounded.Unbounded_String; > end case; > end record; > > Then, if the program assigns a new value to the variable and the value= > has a different Kind, you'd want to be able to rewrite the value > that's stored in the symbol table entry for the variable. In Ada you > can do this by reassigning the entire record. Doing this without > modifiable discriminants would be, I believe, impossible without > adding some access types and a level of indirection, adding the > headaches involved with allocation, deallocation, memory leakage, > dangling references, etc. > > Variant records are the main reason I use default discriminants in Ada= > code. Another typical use is to allow arrays whose size can change > dynamically: > > MAX_LENGTH : constant :=3D 1000; > subtype String_Length is 0 .. MAX_LENGTH; > type Variable_String (Len : String_Length) is record > Value : String (1 .. Len); > end record; > > -- Adam -- = Si les chats miaulent et font autant de vocalises bizarres, c=E2=80=99es= t pas pour = les chiens.