[M3devel] introducing VAR in more places?

Rodney M. Bates rodney.bates at wichita.edu
Tue Feb 12 05:17:13 CET 2008


I don't think this is what Tony meant.  In C++, an opening brace
starts a block, but every local declaration in that block has its own
unique scope that only starts at that declaration and goes to the end
of the block.  This is particularly messy because the local declarations
of a block need not all precede all the statements, and even worse
because the distinction between declarations and statements is extremely
subtle and, unless things have changed, never actually defined.

(Do you remember Stroustrup's famous quote when taken to task about this?
"If it looks like a declaration, it's a declaration.  If it looks like a
statement, it's a statement.  If it doesn't look like either, it's a
syntax error.")

In contrast, in Modula-3, the scope of every local declaration
starts at the beginning of the block and ends at the end of the
block.

The C++ reference makes an attempt at defining different terms for the
entire block and the region where the identifier can be referred-to, i.e.,
from declaration to end of block (which I have called "scope" here).
But as I recall, they ended up carefully defining their terminology in
one section but then not using it consistently in the rest of the
language definition.

One of the few things Ada did better than Modula-3 was that the syntax
has an explicit delimiter at all three relevant points: beginning of block,
between declarations and statements, and end of block.  (But it still has
C++-like scopes for every declaration>.) I am an adamant believer in a high
level of syntactic explicitness.

                              Ada           Modula-3         C++
start of block:              DECLARE       <nothing>        {
between decls and stmts:     BEGIN         BEGIN            <not well-defined, because
                                                              some decls and some stmts
                                                              can be interspersed>
end of block:                END           END              }

Jay wrote:
> If you want a precise scope in C++, just but in an opening brace.
> Just like how in Modula-3 (now I know) you can have "BEGIN" almost 
> anywhere, in C++ you can have an open brace almost anywhere. Ditto in C. 
> It isn't often used though, granted.
>  
>  - Jay
> 
> ------------------------------------------------------------------------
> 
>  > CC: m3devel at elegosoft.com
>  > From: hosking at cs.purdue.edu
>  > Subject: Re: [M3devel] introducing VAR in more places?
>  > Date: Mon, 11 Feb 2008 11:34:54 -0500
>  > To: jayk123 at hotmail.com
>  >
>  > You can, except you need to explicitly give the block scope:
>  >
>  > VAR ...
>  > BEGIN
>  > ...
>  > END;
>  >
>  > I find this much better than C++ where the scope of the new variable
>  > is imprecisely defined.
>  >
>  > On Feb 11, 2008, at 4:29 AM, Jay wrote:
>  >
>  > > Does anyone else find it bothersome that you can't introduce VARs
>  > > anywhere? Kind of like in C++?
>  > > Ok, or if you grant that frame size doesn't matter THAT much, and
>  > > the compiler might do stack packing anyway, you can move all your
>  > > locals up top like typical C, but still, how about for the module
>  > > initializer?
>  > > Still it seems good to me to do manual stack packing since it is
>  > > often so easy..
>  > > WITH helps, but only for "relative constants".
>  > >
>  > > It would be nice if the code below could have still loops until t0 !
>  > > = t1, but the repeat condition is outside the scope in which t1 is
>  > > available. This feels quite lame..
>  > >
>  > > - Jay
>  > >
>  > >
>  > >
>  > > > Date: Mon, 11 Feb 2008 10:24:00 +0000
>  > > > To: m3commit at elegosoft.com
>  > > > From: jkrell at elego.de
>  > > > Subject: [M3commit] CVS Update: cm3
>  > > >
>  > > > CVSROOT: /usr/cvs
>  > > > Changes by: jkrell at birch. 08/02/11 10:24:00
>  > > >
>  > > > Modified files:
>  > > > cm3/m3-libs/m3core/src/time/WIN32/: Time.m3
>  > > >
>  > > > Log message:
>  > > > no need for globals here..
>  > > >
>  > >
>  > >
>  > > Helping your favorite cause is as easy as instant messaging. You
>  > > IM, we give. Learn more.
>  >
> 
> 
> ------------------------------------------------------------------------
> Connect and share in new ways with Windows Live. Get it now! 
> <http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_012008>

-- 
-------------------------------------------------------------
Rodney M. Bates, retired assistant professor
Dept. of Computer Science, Wichita State University
Wichita, KS 67260-0083
316-978-3922
rodney.bates at wichita.edu



More information about the M3devel mailing list