Lazily Cached Property Getters #9272
-
I'm loving the I've found myself writing code like this to avoid unnecessary work/allocations unless called: [field: MaybeNull, AllowNull]
public MyProperty MyProperty => field ??= new MyProperty(OtherProp1, OtherProp2, OtherProp3); The only downside is it's a bit verbose and syntax/attribute heavy. I feel this could be simplified with some syntactic sugar to be something like: public lazy MyProperty MyProperty => new MyProperty(OtherProp1, OtherProp2, OtherProp3); or public MyProperty MyProperty { lazy get => new MyProperty(OtherProp1, OtherProp2, OtherProp3); } Behind the scenes this could do the same thing. Retrieve the backing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You won't need this when teh final form of
TO me this is great. It's clear and simple. The core problem i see with something like |
Beta Was this translation helpful? Give feedback.
You won't need this when teh final form of
field
is allowed. Standard nullability analysis will do "the right thing (tm)" here, and consider the code ou have inside legal. You will have to write:public MyProperty MyProperty => field ??= new MyProperty(OtherProp1, OtherProp2, OtherProp3);
TO me this is great. It's clear and simple. The core problem i see with something like
lazy
is that many developers will have entirely different views on what it would do. And, given that we have a System.Lazy, i think dev will rightly expect the same semantics available there to be present in that language feature.