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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,96ae138aab3beb03 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-31 17:58:44 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: dewar@gnat.com (Robert Dewar) Newsgroups: comp.lang.ada Subject: Re: Localized Variable Declaration Date: 31 May 2002 17:58:43 -0700 Organization: http://groups.google.com/ Message-ID: <5ee5b646.0205311658.52211e12@posting.google.com> References: <3CF77998.9040806@yahoo.com> <3CF77CDA.3090805@yahoo.com> <3CF78D3D.3030400@yahoo.com> NNTP-Posting-Host: 205.232.38.247 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1022893124 13375 127.0.0.1 (1 Jun 2002 00:58:44 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 1 Jun 2002 00:58:44 GMT Xref: archiver1.google.com comp.lang.ada:25134 Date: 2002-06-01T00:58:44+00:00 List-Id: David Rasmussen wrote in message news:<3CF78D3D.3030400@yahoo.com>... > You can do the same thing in C++ by just doing > > int whatever(int foo) > { > ... > ... > { // local scope starts here > int local = 42; > .... > } // and ends here > ... > } Yes, sure, and the interesting thing is that in my experience it is far more common to see extensive use of local declare blocks in Ada than it is in C. I don't know why this is, because indeed narrowing the scope of variables is an important issue for readability. Personally I like a local declarative block being announced clearly with declare, for me the use of { is too overloaded. For example: while (*a++ = *b++) { stuff } and while (*a++ = *b++); { stuff } are uncomfortably close, and I prefer having a very definite keyword that announces a local declare block. As for not allowing declarations in the middle of statements, this is very deliberate for several reasons. Two significant reasons are: The semantics of embedded declarations is nasty with respect to whether they are allowed within control structures, what happens if a goto skips over them, etc. Forcing the use of a declare forces the writer to think about the scope of the declarations that are introduced and end the scopes appropriately. The C++ approach makes it easier for the programmer to ignore this, and by default let the scope extend to the end. Note that it is definitely not the case that C++ can do more than Ada here, just that you have to be noisier about it in Ada. That's a choice we often make, because we unconditionally favor the reader over the writer (for example, consider unchecked conversion vs a cast).