Force Non-Null #2003
-
Often times we write code like this: public Foo _foo;
public Foo Foo
{
get => _foo ?? _foo = new Foo();
set => _foo = value;
}
This:
```csharp
public Foo! Foo; Foo will be never be null, and will return a new copy the instance it's needed. Also note, that adding the |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments
-
As suggested in #2001, other proposals would lead to a nice syntax for this: public Foo Foo
{
set; get => field ??= new Foo();
} and they have the advantage of allowing flexibility around what is assigned when the field is |
Beta Was this translation helpful? Give feedback.
-
@DavidPrio Right, but we could have both, and if we did, I would tend to use public Foo Foo { set; get => field ??= new Foo(); }
public Foo! Foo; We can see that it's also 3x shorter in terms of characters, making it more ideal in the case of |
Beta Was this translation helpful? Give feedback.
-
It also looks like a normal field, not a property. That alone makes it much more difficult to reason about. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Actually, if you include #100, it becomes: public Foo Foo
{
set; get => Foo ??= new();
} @lazyloader which makes the one line version: public Foo Foo { set; get => field ??= new(); }
public Foo! Foo; So not too dramatic of an improvement, but if |
Beta Was this translation helpful? Give feedback.
-
i don't see any value (no pun intended) in trying to make it that much easier to write mutable properties. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi I'm the first person to want to go immutable, but that doesn't sound entirely realistic to me in many cases nor entirely in the paradigm-inclusive spirit of C#. |
Beta Was this translation helpful? Give feedback.
-
I may be putting words in @CyrusNajmabadi's mouth here, but it's not so much "never make it easy to write externally mutable properties", it's a case of "never make externally mutable properties easier to write than externally read-only properties". |
Beta Was this translation helpful? Give feedback.
-
I like that C# in the past hasn't taken the stance that it's a zero-sum game. If a lot of folks are doing something, why not make it easier no matter what other paradigms may or may not be available to them in their particular project? |
Beta Was this translation helpful? Give feedback.
-
"Pit of success"? Make it hard to do bad things, easier to do neutral things and even easier to do good things. (Though I feel like I've wondered into the realms of D&D's alignment system here, rather than programming 😃) |
Beta Was this translation helpful? Give feedback.
-
Deciding that mutability itself is a bad thing is not paradigm-inclusive, in line with what I would have said the spirit of C# is. |
Beta Was this translation helpful? Give feedback.
-
I'd rather make other stuff easier :)
That wasn't my argument. :) I was simply saying that i don't see a lot of value in making this pattern easier. You can still implement the pattern today, without too much muss or fuss. That seems a good enough state to be in for me. |
Beta Was this translation helpful? Give feedback.
-
Since C# is heavily used for a lot of stuff including GUI I think it actually makes total sense and is valuable. (As it seems, swift included didSet willSet mostly for that single use case). Also, as for |
Beta Was this translation helpful? Give feedback.
-
The OP seeks to enshrine a very specific special case: A lazily populated property
I can see that this isn't an astonishingly rare situation, but I don't see it being common enough to justify the overwhelming cost of a language change, especially since the syntax currently available in C# (i.e. already available for use by all) is clean, concise, and straightforward. |
Beta Was this translation helpful? Give feedback.
-
This seems to me to be solved with a very oft-requested feature for lazily initialized properties (although I seem to remember that those have unfortunately been shot down in the past due to inability to decide on locking mechanisms):
|
Beta Was this translation helpful? Give feedback.
As suggested in #2001, other proposals would lead to a nice syntax for this:
and they have the advantage of allowing flexibility around what is assigned when the field is
null
, rather than hard-codingnew Foo()
as your proposal would do.