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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!au2pb.net!feeder.erje.net!2.us.feeder.erje.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Substrings as argument to procedures/functions Date: Tue, 12 Apr 2016 14:17:42 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <2055a188-fb5f-496a-ab37-b25d81cebe1b@googlegroups.com> NNTP-Posting-Host: shell02.theworld.com Mime-Version: 1.0 Content-Type: text/plain X-Trace: pcls7.std.com 1460485036 23108 192.74.137.72 (12 Apr 2016 18:17:16 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Tue, 12 Apr 2016 18:17:16 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:3TW5jgul66tx1sgWZQ2+1jKWTOs= Xref: news.eternal-september.org comp.lang.ada:30084 Date: 2016-04-12T14:17:42-04:00 List-Id: reinkor writes: > I believed that if a substring is an argument to a subroutine, > then its "range" would start at 1 (and not inherited from the calling program). > But this seems not to be the case. > > Given the following Ada source code: > > with Text_IO; > use Text_IO; > procedure t3 is > package Int_Io is new Text_IO.Integer_Io (Integer); > use Int_Io; > procedure string_test1(S : String) is > begin > New_Line; > Put(" In string_test1: "); > Put(S'First,4); > Put(S'Last,4); > end string_test1; > Str : String := "ABCDEF"; > begin > string_test1(Str); > string_test1(Str(3..5)); > end t3; > > The second call to "string_test1" gives: > > In string_test1: 3 5 > > (at least on my computer). > > Is this correct? Yes. >...And in case, is it "good" ? :-) No. It breaks abstraction -- the procedure shouldn't have to know where the string came from (e.g. a slice). And it either introduces bugs (because the code assumes S'First = 1, which is almost always true), or it complicates the code (because you have to do arithmetic on the bounds). But Dmitry makes a good point. - Bob