-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local Extension Methods #799
Comments
Related issues about expanding where extension methods can be declared: dotnet/roslyn#16271, dotnet/roslyn#4565 and #301. |
I don't know if I'd use this, but I'd definitely use static local methods. |
What would you use static local methods for? I can't (yet, until you enlighten me) imagine a use-case for them. |
In the normal case local methods are already static. Their code generation is quite different from that of lambdas, unless you also have a lambda in the same method that captures scope. public void M() {
int x = 1;
int y = 2;
int z = add(3);
int add(int p) => x + y + p;
}
// translated into
[CompilerGenerated]
[StructLayout(LayoutKind.Auto)]
private struct <>c__DisplayClass0_0
{
public int x;
public int y;
}
public void M()
{
C.<>c__DisplayClass0_0 <>c__DisplayClass0_;
<>c__DisplayClass0_.x = 1;
<>c__DisplayClass0_.y = 2;
C.<M>g__add0_0(3, ref <>c__DisplayClass0_);
}
[CompilerGenerated]
internal static int <M>g__add0_0(int z, ref C.<>c__DisplayClass0_0 ptr)
{
return ptr.x + ptr.y + z;
} What would marking a local function as |
@DavidArno There have been a whole bunch of times where I refactored parts of a method implementation into static helper methods and then wished I could restrict the visibility to the sole method that needs it because the helper method is really an implementation detail meaningless outside the implementation of that one method. I begrudgingly polluted the class's large method list rather than making the method non-static and local, because making it a local method didn't enforce method purity with regard to instance state and I really value that guarantee when reading code. I have a strong desire similarly to nest types inside methods when those types are no more than some method's implementation details. It just keeps the class clean. |
@HaloFour It's not for performance, it's for code organization. Restricting what you can do inside the method as you write code, restricting the visibility of an implementation detail to just where it makes sense, and signaling the coding style to the reader. |
That doesn't really answer my question. What difference does marking the local function as |
You did indeed enlighten me 😁 Upvoted therefore as I agree that would be useful. |
Let me sort of correct myself there. If the local function does capture My understanding of local functions is that they aren't considered "methods" in that they don't really belong to any type. They're logically more like top-level functions that happen to be scoped to only within the declaring method. They support closures via lexical scope but otherwise they are completely independent of the type and method in which they are defined. |
It provides a compiler error if you reference an instance member or a local or parameter. They won't show up in intellisense. Code you refactor into the static local method will error if you missed something.
Ouch, I'm not sure how I failed you here. 😃 The answer I have is a restatement of what I said already. |
@HaloFour Yes, contrary to what I thought back when I saw #275, I guess there have been a few times when I've thought about expressing a static lambda. Both would be cool but if I had to choose I'd definitely pick local methods. |
I wonder if this would go well with @tannergooding's static delegates. |
Thread jacked, closing issue. |
@scalablecory I'm sorry, not my intention. Did you want local extension methods to be non-static, able to capture locals and parameters? |
I'd like to see local methods support the same
this
syntax to create extension methods. So this:Could be written instead as:
This currently fails saying that extension methods must defined in a static class. I see no reason to disallow it in local methods.
The text was updated successfully, but these errors were encountered: