scoped methods and variables #1643
-
basically this public class Foo
{
{
int x = 43; // can only be used in this scope
void Bar() { } // can only be used in this scope
}
void Baz()
{
// can't use x or Bar
}
} this will make code more simple and easy to understand. by using this scope blocks to hide members from the other parts of the class. |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments
-
I don't understand, if the rest of the code can't use |
Beta Was this translation helpful? Give feedback.
-
This already works just as you'd expect if you move that block from the type level into the block of a type member: public class Foo
{
void Baz()
{
...
{
int x = 43;
void Bar() { }
}
...
}
} Having that scope at the type level doesn't appear to make much sense because it's completely unclear when the code in that local scope would run. If you want it to run upon object instantiation, just put it inside the constructor: public class Foo
{
public Foo()
{
...
{
int x = 43;
void Bar() { }
}
...
}
} Nested scopes can sometimes be useful to reduce cluttering of the outer scope with variable declarations, or to avoid name collisions. That said, your example doesn't make much sense because you have a variable that you never use, and a local function that you never call. If I may make a suggestion, if you propose a new syntax, it would be a good idea to include a realistic example of how it would be useful, and talk people through it a little bit. 😉 |
Beta Was this translation helpful? Give feedback.
-
@lazyloader using the new feature "local functions" in c# 7 you can create functions only visible inside the method, is more or less the same thing. |
Beta Was this translation helpful? Give feedback.
-
I tried to keep the example brief, so let me add more to it. Imagine this code public class Foo
{
{
int x = 43; // can only be used in this scope
void Bar(int z) { } // can only be used in this scope
void Fizz() => Bar(34);
void Buzz() => x * 2;
}
void Baz()
{
// can't use x or Bar
}
void FooBar() => Buzz(); // error, Buzz() is out of scope
} Local functions only show themselves to one function, but these scopes, the functions are local to two or more functions, but not any outside of it, giving better encapsulation |
Beta Was this translation helpful? Give feedback.
-
All of your examples so far seem to show members that can't be called/referenced from anywhere at all. So what's the point? 😕 |
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek That's not true. In the comment I made, I show It's literally the same thing as block bodies inside methods. It's not a foriegn concept. |
Beta Was this translation helpful? Give feedback.
-
Yes. But how will you use anything in that "scope" from the outside world? 😄 |
Beta Was this translation helpful? Give feedback.
-
No it's not. Control flow will enter a block through normal execution. But there's no way to execute any of the code inside that "scope" in your example. |
Beta Was this translation helpful? Give feedback.
-
@lazyloader, I'm trying to understand how you think this feature is supposed to work. If it was obvious from your example, I wouldn't have asked for clarification. Here's one of your examples:
The nested scope contains four members - They're all declared in exactly the same way - none of them have any explicit visibility modifiers - so I'd expect them to have the same visibility as each other. You explicitly show that Can If If |
Beta Was this translation helpful? Give feedback.
-
@lazyloader Maybe another way to ask the question everyone is asking would help: Can you show a small, but complete program (i.e. including |
Beta Was this translation helpful? Give feedback.
-
Just use a nested class- public class Foo
{
class Scoped
{
int x = 43; // can only be used in this scope
void Bar(int z) { } // can only be used in this scope
void Fizz() => Bar(34);
int Buzz() => x * 2;
}
void Baz()
{
// can't use x or Bar
}
void FooBar() => Buzz(); // error, Buzz() is out of scope
} |
Beta Was this translation helpful? Give feedback.
-
Considered in https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-04-01.md |
Beta Was this translation helpful? Give feedback.
-
@Unknown6656 #140 (use backing field without naming it) was discussed in that meeting, but this issue has overlap with #133 (declare normal fields with property scope) which is a different and partially independent request. |
Beta Was this translation helpful? Give feedback.
I don't understand, if the rest of the code can't use
x
orBar
, how are they useful?