Re: already used in a 'child' scope to denote something else
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Fri, 08 Jun 2007 00:18:48 -0700
On Jun 8, 7:59 am, "valentin tihomirov" <V_tihomi...@xxxxxxx> wrote:
{
int i = 2;
}
int i = 1;
There is no 'i' defined in the 'parent' context from the moment of
declaration on. So what is the problem?
From section 10.7 of the spec:
<quote>
The scope of a local variable declared in a local-variable-declaration
is the block in which the declaration occurs.
</quote>
In other words, the "outer" variable i has scope which includes the
part of the block before it was declared. There's then an extra rule:
<quote>
Within the scope of a local variable, it is a compile-time error to
refer to the local variable in a textual position that precedes the
local-variable-declarator of the local variable.
</quote>
The rationale is given in notes of the spec:
<quote>
Note: The scoping rules for local variables are designed to guarantee
that the meaning of a name used in an expression context is always the
same within a block. If the scope of a local variable were to extend
only from its declaration to the end of the block, then in the example
above, the first assignment would assign to the instance variable and
the second assignment would assign to the local variable. In certain
situations but not in the exampe above, this could lead to a compile-
time error if the statements of the block were later to be rearranged.
</quote>
They tell us they pursue language
simplicity. The rule "do not define a variable more than once in the same
context" is natural, and simplest therefore. All normal languages obey it
therefore. Overcomplicating a grammar by injecting more barrieres is a path
right away from simplicity.
Another obstacle at the plain place introduced
in C# for no reason is blocking "fall throughs" in switch -- the feature
wich can be very useful sometimes.
Very useful sometimes when it's deliberate, but also very painful when
you don't want it and accidentally have it. Note that you *can* use
multiple cases for a single block, you just can't have case/code/case/
code without a break.
I suspect that when working with C, I ran into more times when I
forgot to include the break than times when I deliberately wanted to
fall through.
The switch statement isn't very nicely designed in general, however -
it harks back to C too much, IMO. I would rather have seen cases
require an extra set of braces, and for fallthrough you could then
have an explicit "continue" instead of a break for non-fallthrough.
So yes, I agree that switch isn't great - but taking away the
restriction against fallthrough would make it even worse, IMO.
C# designers demonstrate some excessive
zeal on 'protecting' us from doing things naturally, going strightforward
ways.
Well, I'm happy with both of these decisions.
"Give a fool rope enough and he'll hang himself."
True. The less rope you provide, the more foolish he has to be
though...
Jon
.
- Follow-Ups:
- Re: already used in a 'child' scope to denote something else
- From: Ben Voigt [C++ MVP]
- Go ahead. Stop programming. This ensures you from any mistakes.
- From: valentin tihomirov
- Re: already used in a 'child' scope to denote something else
- References:
- already used in a 'child' scope to denote something else
- From: valentin tihomirov
- already used in a 'child' scope to denote something else
- Prev by Date: Re: Launch devenv.exe and open a web site?
- Next by Date: Re: VistaDB Database
- Previous by thread: already used in a 'child' scope to denote something else
- Next by thread: Go ahead. Stop programming. This ensures you from any mistakes.
- Index(es):
Relevant Pages
|