Skip to content

Proposed speclet for static lambdas#2899

Merged
jcouv merged 1 commit intomasterfrom
dev/jcouv/static-lambda
Oct 22, 2019
Merged

Proposed speclet for static lambdas#2899
jcouv merged 1 commit intomasterfrom
dev/jcouv/static-lambda

Conversation

@jcouv
Copy link
Member

@jcouv jcouv commented Oct 21, 2019

Championed issue #275
Relates to PR dotnet/roslyn#39121

@jcouv jcouv self-assigned this Oct 21, 2019
@jcouv
Copy link
Member Author

jcouv commented Oct 21, 2019

Tagging @cston @CyrusNajmabadi @gafter for review.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree with this. Should we consider something additional like:

Implementations are recommended to emit a static lambda with a singleton allocation for improved performance

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine to leave that as an implementation detail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't that be useful with function pointers?

unsafe class Instance {
    void Use() {
        delgate*<Instance, string> f = &static i => i.ToString();
        f(this);
    }
}

In the proposal, the recommended workaround for instance methods is to use static local functions. I think a major advantage of using static lambdas over static local functions here is type inference and avoiding typing out the whole signature so it could be of help in this scenario as well.

@jcouv jcouv merged commit e9afb74 into master Oct 22, 2019
@jcouv jcouv deleted the dev/jcouv/static-lambda branch October 22, 2019 17:11
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you intend that such a reference is a compile-time error. Please say that, and identify the sections of the ECMA specification that require modifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants