From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 8 Jul 91 16:42:00 GMT From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!th ink.com!snorkelwacker.mit.edu!bu.edu!inmet!stt@ucbvax.Berkeley.EDU Subject: Re: Sliding Semantics? Message-ID: <20600109@inmet> List-Id: > Written Jul 3, 1991 by nwebre@polyslo.CalPoly.EDU in inmet:comp.lang.ada */ > /* ---------- "Sliding Semantics?" ---------- */ > > We have run into a totally unexpected problem involving an obscurity in > Ada called "sliding semantics ". > . . . > with text_io; use text_io; > procedure main is > subtype index is natural range 0 .. 80; > type udr_string(length : index := 0) is record > text : string (1 .. length); > end record; > s1, s2, s3 : udr_string; > posn, len: natural; > > begin > s1 := (4, "abcz"); > s2 := (5, "defgh"); > posn := 3; > len := 2; > > -- Delete len bytes starting at posn from s3. > -- CONSTRAINT_ERROR when posn is 1. > s3 := udr_string'(s3.length-len, s3.text(1..posn-1) & > s3.text(posn+len..s3.length)); No "sliding" takes place for an aggregate component. This constraint error is due to the result of concatenate taking the low bound from the right operand (posn+len) when the left operand is null (posn-1 = 0). > -- Place a substring of s2 starting at posn and extending length len into s3. > -- CONSTRAINT_ERROR when posn anything but 1. > s3 := (len, s2.text(posn .. posn + len -1)); No "sliding" here either. Constraint error if posn /= 1. > . . . > Does anyone know the rules or where I can find the rules on when sliding > semantics apply? Sliding occurs only in assignment statements (see RM 5.2.1(1)), in array object initializations (see RM 3.2.1(16)), and upon explicit array (sub)type conversion (see RM 4.6(11)). > There are a couple of workarounds. The best we've been able to invent > has been using type conversions. The substring statement, for instance, > becomes > > declare > type anchor is new string(1..len); > begin > s3 := (len, string(anchor(s2.text(posn .. posn + len -1)))); > end; > > That "slides" the index range of the result to be from 1 to some value. > > Does anyone have a better solution? A somewhat simpler solution is to make "anchor" into a constrained subtype: declare subtype anchor is string(1..len); begin s3 := (len, anchor(s2.text(posn .. posn + len -1))); end; One could also define a function "Slide": function Slide(S : String) return String is subtype Anchor is String(1..S'Length); begin return Anchor(S); end Slide; pragma Inline(Slide); This whole area is one of the more annoying little back alleys of Ada 83. For Ada 9X we have proposed to allow implicit subtype conversion (aka "sliding") any place where there is an "applicable index constraint" (see 4.3.2(4-8)), other than on an OUT or IN OUT parameter. We resolve the interaction with aggregates with named associations and an others clause (see RM 4.3.2(6)) by specifying that implicit subtype conversion is never applied to such an aggregate. This caveat replaces the restriction in paragraph RM 4.3.2(6) completely. These new rules would allow your original examples to work as originally written. (As usual, this Ada 9X proposal is not yet finalized, so no promises, though comments are welcome...) > Neil Webre > Cal Poly, San Luis Obispo, CA, USA > Department of Computer Science S. Tucker Taft stt@inmet.inmet.com Ada 9X Mapping/Revision Team Intermetrics, Inc. Cambridge, MA 02138