Update and rename static-lambdas.md to static-anonymous-functions.md (#3651)

This commit is contained in:
Rikki Gibson 2020-07-08 17:06:50 -07:00 committed by GitHub
parent d96d221393
commit d0deb2f61b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 30 deletions

View file

@ -0,0 +1,32 @@
# Static anonymous functions
## Summary
Allow a 'static' modifier on lambdas and anonymous methods, which disallows capture of locals or instance state from containing scopes.
## Motivation
Avoid unintentionally capturing state from the enclosing context, which can result in unexpected retention of captured objects or unexpected additional allocations.
## Detailed design
A lambda or anonymous method may have a `static` modifier. The `static` modifier indicates that the lambda or anonymous method is a *static anonymous function*.
A *static anonymous function* cannot capture state from the enclosing scope.
As a result, locals, parameters, and `this` from the enclosing scope are not available within a *static anonymous function*.
A *static anonymous function* cannot reference instance members from an implicit or explicit `this` or `base` reference.
A *static anonymous function* may reference `static` members from the enclosing scope.
A *static anonymous function* may reference `constant` definitions from the enclosing scope.
`nameof()` in a *static anonymous function* may reference locals, parameters, or `this` or `base` from the enclosing scope.
Accessibility rules for `private` members in the enclosing scope are the same for `static` and non-`static` anonymous functions.
No guarantee is made as to whether a *static anonymous function* definition is emitted as a `static` method in metadata. This is left up to the compiler implementation to optimize.
A non-`static` local function or anonymous function can capture state from an enclosing *static anonymous function* but cannot capture state outside the enclosing *static anonymous function*.
Removing the `static` modifier from an anonymous function in a valid program does not change the meaning of the program.

View file

@ -1,30 +0,0 @@
# Static lambdas
## Summary
Support lambdas that disallow capturing state from the enclosing scope.
## Motivation
Avoid unintentionally capturing state from the enclosing context.
## Detailed design
A lambdas with `static` cannot capture state from the enclosing scope.
As a result, locals, parameters, and `this` from the enclosing scope are not available within a `static` lambda.
A `static` lambda cannot reference instance members from an implicit or explicit `this` or `base` reference.
A `static` lambda may reference `static` members from the enclosing scope.
A `static` lambda may reference `constant` definitions from the enclosing scope.
`nameof()` in a `static` lambda may reference locals, parameters, or `this` or `base` from the enclosing scope.
Accessibility rules for `private` members in the enclosing scope are the same for `static` and non-`static` lambdas.
No guarantee is made as to whether a `static` lambda definition is emitted as a `static` method in metadata. This is left up to the compiler implementation to optimize.
A non-`static` local function or lambda can capture state from an enclosing `static` lambda but cannot capture state outside the enclosing `static` lambda.
Removing the `static` modifier from a lambda in a valid program does not change the meaning of the program.