[M3devel] introducing VAR in more places?

hendrik at topoi.pooq.com hendrik at topoi.pooq.com
Tue Feb 12 14:23:26 CET 2008


On Mon, Feb 11, 2008 at 10:17:13PM -0600, Rodney M. Bates wrote:
> 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,

That is, in my opinion, one of the few things that C and C++ did right, 
well, almost right.  It permits a coding style in which every 
variable declaration is initialised, and is declared if and only if it 
has a value.  Now that's a useful property.  It fails for recursion, 
so there are limits in how far it can apply.  And when coding like this, 
you want the simplest syntax to define a constant identifier -- one 
whose value cannot be rebound except by reexecuting the entire block.  
Making something that can change should require more effort -- like 
adding a keyword "var" or some such.

It's amazing how many identifiers end up fitting this constraint, 
yielding very little visual ambiguity as to whether the value of a 
variable has changed since initialisation.

This tends to be the style in functional languages.  In my opinion, 
functional-style languages would benefit from detailed static typing (as 
detailed as Modula 3) and some imperative features, like 
assignment to things that are explicitly declared as being variable.  
While rarely needed (but usually at least a few times per program), 
doing completely without imperative features I consider an exercise in 
masochism.

But I can't see any way that this would fit seamlessly into Modula 3 
syntax.

-- hendrik

> 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.")

Last time I looked at it (when I was involved in a C++ implementation a 
decade or more ago) the rule was "If it looks like a declaration, it's a 
declaration; *otherwise* if it looks like a statement, it's a 
statement.", with precise constraints on what contextual information the 
compiler was allowed to look at when deciding what it looked like.  The 
only correct way I found to implement this was to parse every 
statement-declaration-like thing first as a declaration, and, if that 
failed, as a statement.

It was a mess.

-- hendrik

> 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.

A simple rule, but, in my opinion, a little too simple.

> 
> 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.

Their heart wasn't really into it, was it?  Much rather let the 
implementation define the language.

-- hendrik



More information about the M3devel mailing list