Interesting difference between delegates and local functions #392
Replies: 11 comments
-
I listened a some podcast I can't remember from where, but it said local functions use some sort of black magic so avoid allocations do inlining etc. they are not same as Also as an side note you should always keep lock scope small as possible anyway. You shouldn't be creating |
Beta Was this translation helpful? Give feedback.
-
I don't think the C# compiler inlines functions. Does it? It's the JIT compiler that inlines small functions. But moving the function definition does not change "inlinability" or the scope of the lock. And it compiles. |
Beta Was this translation helpful? Give feedback.
-
It's true that local functions avoid allocations by sometimes using a But I don't see how is any of that relevant here. |
Beta Was this translation helpful? Give feedback.
-
@svick Your are probably right and issue is probably that roslyn's await lock check doesn't understand local functions. |
Beta Was this translation helpful? Give feedback.
-
Local function doesn't use |
Beta Was this translation helpful? Give feedback.
-
Nevertheless, no code under the lock is being awaited. It's just being defined and not closing over the locked object. And that would be, at most, a concurrency problem. |
Beta Was this translation helpful? Give feedback.
-
yield is not allowed in finally (just local functions since we don't have iterator lambdas) finally {
IEnuemrable<int> Local() {
yield return 1; // ERROR
}
} or await in unsafe (in both cases), unsafe {
Func<Task> f = async () => await Task.Yield(); // ERROR
Task Local() => await Task.Yield(); // ERROR
} so there might be a good reason for that, but since your example does compile with a lambda I suspect it's a bug. |
Beta Was this translation helpful? Give feedback.
-
This looks like a https://github.com/dotnet/roslyn bug. |
Beta Was this translation helpful? Give feedback.
-
Agree that this looks like a bug. |
Beta Was this translation helpful? Give feedback.
-
A compiler bug or a spec bug? |
Beta Was this translation helpful? Give feedback.
-
This compiler bug has been reported at dotnet/roslyn#18644 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Preparing for C# 7 while coding in C# 6, I decided that it was good enough to use some delegates for now in order to have the code prepared for C# 7 local functions in the near future.
I wrote something like this:
That would be, in the future, converted to:
Except that I now get a CS1996 error.
But this compiles:
/cc @MadsTorgersen @gafter
Beta Was this translation helpful? Give feedback.
All reactions