Re: already used in a 'child' scope to denote something else



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

.



Relevant Pages

  • Re: question about assigning null to a reference object
    ... for local variables, then they are not the same. ... The following code will also fail to compile, ... it is best to not initialize it at all. ... them instinctively initialize all variables at their declaration. ...
    (comp.lang.java.help)
  • Re: Static variables in ASP .NET/VB .NET
    ... Specifies that one or more declared local variables are to remain in existence and retain their latest values after termination of the procedure in which they are declared. ... This means the declaration context for a Static variable must be a procedure or a block within a procedure, and it cannot be a source file, namespace, class, structure, or module. ... You cannot use Static inside a structure procedure. ... You call a Shared procedure using the class name, not a variable pointing to an instance of the class. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: simple example
    ... I still don't fully understand why this cause compile error. ... This is before this message is for *local variables* (which is what you ... member that has the same name in a parent scope which is allowed). ... local variable declaration inside the function is meaningfull (i.e. before ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reusing the name of local variable
    ... it's not accessible before its declaration. ... Doesn't compiler see that these are two different variables? ... It considers them to be two different local variables with the same ... with one of them in the scope of the other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: simple example
    ... This is unrelated with static int x=123. ... So using x even before the declaration means that you are trying to use ... A local variable named 'x' cannot be declared in this scope because it would ... you still have two x local variables named the same way in ...
    (microsoft.public.dotnet.languages.csharp)