Skip to content
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

Proposal: Null-conditional Assignment (Lazy Initialization Operator) #5163

Closed
alrz opened this issue Sep 11, 2015 · 5 comments
Closed

Proposal: Null-conditional Assignment (Lazy Initialization Operator) #5163

alrz opened this issue Sep 11, 2015 · 5 comments

Comments

@alrz
Copy link
Member

alrz commented Sep 11, 2015

Currenty (no concurrency check):

public Foo Foo => _foo ?? (_foo = new Foo());

Null-conditional Assignment:

public Foo Foo => _foo ?= new Foo();

This can be implemented in a way that properly use the System.Threading.LazyInitializer class.

@HaloFour
Copy link

Dupe of #3366.

@alrz
Copy link
Member Author

alrz commented Sep 11, 2015

@HaloFour Sorry I didn't see it, since it's closed. They didn't mention LazyInitializer though, nor the use case I suggested here.

@HaloFour
Copy link

Well there is #3630 specifically for lazy initialization using a decorator on property signatures. Using the operator syntax makes it feel like it is much more general purpose. The fact that it could be used to emulate lazy behaviors in properties I don't think matters much, and if it would behave differently than a null assignment in that context I think that would make it confusing.

@alrz
Copy link
Member Author

alrz commented Sep 11, 2015

@HaloFour Lazy initialization is not bounded to properties. you still want to use it in other places. what about initializing a private field in some method? It is general purpose. you can use them everywhere for initializing members. and it just works — thread safely.

@alrz
Copy link
Member Author

alrz commented Mar 26, 2016

This is neatly covered by replace/original feature.

public class Foo {
  [Lazy]
  public object Value => GetValue();
}

partial class Foo {
  object mValue;
  replace object Value => mValue ?? (mValue = original);
}

Thread safety and other stuff is up to the code gen provider.

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

No branches or pull requests

4 participants