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-Thread: a07f3367d7,4ce5890331a5b529 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!x42g2000yqx.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Discriminants of tagged types Date: Wed, 27 Oct 2010 13:26:57 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <14314714-e92c-4036-9cbb-da8e72489261@h7g2000yqn.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1288211218 10974 127.0.0.1 (27 Oct 2010 20:26:58 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 27 Oct 2010 20:26:58 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: x42g2000yqx.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:14857 Date: 2010-10-27T13:26:57-07:00 List-Id: On Oct 27, 8:58=A0am, Yannick Duch=EAne (Hibou57) wrote: > Indeed, this show very well how can default discriminants be a mess (and = =A0 > there are other reasons too to add to the ones given here). > > A question then come : do someone know something to which default =A0 > discriminants are unavoidable ? I feel default discriminants can simply b= e =A0 > dropped (kind of sugar?), but I would like to be sure. 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