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=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4c86cf2332cbe682 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-18 06:27:40 PST Path: nntp.gmd.de!Germany.EU.net!wizard.pn.com!sundog.tiac.net!news.kei.com!news.mathworks.com!uunet!newsgate.watson.ibm.com!watnews.watson.ibm.com!ncohen From: ncohen@watson.ibm.com (Norman H. Cohen) Newsgroups: comp.lang.ada Subject: Re: Ada.strings.bounded problems? Date: 18 Jan 1995 14:27:40 GMT Organization: IBM T.J. Watson Research Center Distribution: world Message-ID: <3fj8gs$h9b@watnews1.watson.ibm.com> References: <3f3rl7$kts@watnews1.watson.ibm.com> <3fduto$ta7@watnews1.watson.ibm.com> Reply-To: ncohen@watson.ibm.com NNTP-Posting-Host: rios8.watson.ibm.com Date: 1995-01-18T14:27:40+00:00 List-Id: In article , Mats.Weber@matrix.ch (Mats Weber) writes: |> In article <3fduto$ta7@watnews1.watson.ibm.com>, ncohen@watson.ibm.com wrote: |> ... |> |> > Ada 95 allows predefined equality to be overridden, even for nonlimited |> > types, so the declaration of a type Bounded_String with a discriminant |> > specifying the maximum length should be accompanied by the declaration |> > for the following function: |> > |> > function "=" (Left, Right: Bounded_String) return Boolean is ... |> |> This helps some, but still has severe problems. For instance if I write |> |> type Pair is |> record |> First, |> Second : Bounded_String; |> end record; |> |> then "=" is broken for Pair. This situation is very bad because overriding |> "=" on Bounded_String gives the programmer the feeling that the problem is |> solved, but it reappears whenever a Bounded_String is used as a component |> of a composite type. Yes, but Ada 9X provides a straightforward solution for this too. If a private type is implemented as a tagged type with user-defined "=", then equality tests for composites containing the private type invoke the user-defined "=". (See RM95 4.5.2(15) and 4.5.2(24). In paragraph 24, it is unclear whether a component belonging to a private type with a tagged implementation is a "tagged component", but because of paragraph 15, it doesn't matter.) Thus, what we have to write is: package Interoperable_Bounded_Strings is type Bounded_String (Max_Length: Natural) is private; function "=" (Left, Right: Bounded_String) return Boolean; ... private type Bounded_String (Max_Length: Natural) is tagged record Current_Length : Natural; Text : String (1 .. Max_Length); end record; end Interoperable_Bounded_Strings; Because we have added the word "tagged" to the full-type declaration, predefined "=" for type Pair will now invoke user-defined "=" for type Bounded_String. (This is not the usual use for tagged types, since, even though the full type can be extended in the body or in a private child package, this is unlikely to happen. In this case, the type is declared tagged solely so that the new composition rules for "=" will apply to it. Tucker Taft will correct me if I am wrong, but I believe that these are the rules he would have liked to apply to ALL types if he had been starting with a blank slate. Upward compatibility with Ada 83 constrained him to "do the right thing" only for tagged types, which of course do not arise in Ada-83 programs.) -- Norman H. Cohen ncohen@watson.ibm.com