Variable shadowing in same scope / reusable variable names #6098
Replies: 8 comments 12 replies
-
I personally don't like this behavior in those languages that allow it. I think that names are cheap and it's worth it having separate identifiers for the sake of clarity. There are some cases where the scope of identifiers does create a nuisance but I'd rather address that by allowing redeclaration where definite assignment does not overlap, which will help in scenarios such as pattern matching with leaky scope. |
Beta Was this translation helpful? Give feedback.
-
I'm slightly amenable to work in this space. However, a hard requirement for me would be some sort of syntax that makes it clear that you are intending to shadow a variable. That way accidental collisions keep getting blocked, but the user has a way to express intent here that allows for this. |
Beta Was this translation helpful? Give feedback.
-
Eric Lippert had a whole series looking at the reasons behind the current behaviour of C# and how he improved the error messages in the Roslyn compiler. In one of those he wrote:
Eric doesn't specify which language was in use when he did this - but it may have been C++. The series is very much worth a read. |
Beta Was this translation helpful? Give feedback.
-
I have to admit I don't really understand the object that a lot of people have with variable shadowing, but perhaps that's because I don't happen to have been bitten by it in the past. I, for one, really appreciate being able to have a variable that replaces another in F#, such as: let m(items: IEnumerable<_>) =
let items = items.ToList() // I'm doing this so that I can change the type of the existing variable `items`
... |
Beta Was this translation helpful? Give feedback.
-
I'd like to shadow variables if and only if the type is exactly the same. I mostly encounter it inside conditionals and |
Beta Was this translation helpful? Give feedback.
-
Coming here to say that at least, in C# you can partially shadow fields (you can still access them with qualifier, so it is not true shadowing in a sense), and that variable shadowing has legit usages (fsharp/fslang-suggestions#1299). It can always come with "on by default" warnings if the language design, from the team & community perspective, feels more appropriate to the ethos of C#, than enabling it in full. I think, going this direction, this would also requires reconsideration on ambiguous resolution for symbols and namespaces, to be more graceful when there is no ambiguity based on the type checking. For example, I happen to have a I think it is fine for C# to loosen a bit (with warnings for shadowing, rather than error) and be more conservative in what it disallow, if there is no ambiguity, in general sense. Upvoting this suggestion. |
Beta Was this translation helpful? Give feedback.
-
Just a food for thought, C# doesn't presently even allow this: ...
int x=0;
for (int i = 0; i < 1; i++) {
int x = 5;
}
Console.WriteLine (x);
... That of course won't compile, but this, or the named function version will: ...
int x = 0;
new Action(() => {
for (int i = 0; i < 1; i++) {
int x = 5;
}
})();
Console.WriteLine (x);
... ...and prints 0 as expected. When the language was created, local functions weren't allowed, so this wasn't an issue, but now the contrast seems funny to me. In these cases, and even the OP, I think the intent to shadow is quite explicit, but a reviewer or unsuspecting editor accustomed to old C# ways may not be on the lookout for it, not even in the case I show. I'm not sure anyone ever would be in the cases the OP shows. Anyway, I would assume that if shadowing were added, it would have to allow for this less controversial example as well. |
Beta Was this translation helpful? Give feedback.
-
I can't think of a single usage for true variable shadowing that doesn't border on just promoting lazy naming conventions. Every use case you gave could benefit from better names. For cases 1 and 2, the first definition of From the Wikipedia article you linked:
Emphasis mine. Why would you want to promote something, that in its very definition, claims to leads to confusion? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Some languages allow variable shadowing where the two variables are in the same scope. For example, Rust:
It would be nice if C# allowed this as well.
Examples
Case 1: Variable re-definition
Case 2:
out
variableCase 3: Casting
Case 4:
as
expressionsCase 5:
is
expressionsExamples 5 and possibly 2 require control flow analysis so they might be too hard but the basic idea is that some of these examples would work.
Possible problems
Problem 1: Mutable values
Problem 2: Nested out variables
Problem 3: Lambdas
Problem 4: Disposable values
Beta Was this translation helpful? Give feedback.
All reactions