-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: Property initializer for readonly properties #9516
Comments
Your implementation would make it impossible to explicitly pass a |
I also don't see any issue in optional constructor argument names actually being optional. If I want to save a few keystrokes and rely on positional construction (which is, ya know, the default), why shouldn't I be able to? |
class Foo {
public object Optional { get; } = null;
public Foo(...) {}
// translates to
public Foo(object Optional = null) {
this.Optional = Optional ?? null;
// however, it can be optimized to the following because the default is null
this.Optional = Optional;
}
// and then
new Foo(Optional = null)
They already do,
So why would you use named arguments in attributes in the first place, you could define all of them in the constructor (perhaps, as optional arguments), right? |
I meant for the consumer of said attribute. If the default was If object initialization syntax was considered in the C# 1.0 timeframe it's possible that we would have seen different syntax for attributes. Object initialization does require mutable properties, as does attribute initialization via properties. Said initialization has its advantages over default parameter values given that the latter requires that the value be embedded at the call site and you can't really detect an omitted value vs. a specified default value. |
@HaloFour If a null is desired, the default should have been null too because immutable properties are not meant to be changed. perhaps you should consider a full properties declaration for that matter, e.g. get { if(field == null) { field = new object(); } return field; } It doesn't make sense to provide a syntax for every possible situation.
So you're against #229 too, right? because the point is to initialize readonly auto properties without an explicit constructor declaration with parameters and initializations. |
No, if an explicit string TestOptional(string value = "default") => value;
...
Debug.Assert(TestOptional(null) == null);
I find it unnecessary. I don't think I'm the only one: #9330 (comment) |
Consider the following attribute.
Currently, for declaring this attribute you may write something like this:
Because you wanted
Optional
property be, well, optional, you are forced to make it mutable.Instead, you could write it like this:
But ...
And things can get weirder,
Actually, it might be unfortunate that despite of the fact that you are assigning
Optional
inside of the constructor argument list, it gets evaluated after that, just like property initializers, unless you will know that because property initializers are enclosed in{ }
separate from the constructor.The idea is to allow such immutable properties to be initialized by name inside of the constructor parameter list, optionally; and this would not be just for attributes — making the syntax more consistent, meaningful and widely usable.
Under this proposal,
...
generates optional arguments for each property that has an initializer.In fact, it doesn't matter that what value it is initialized with, because the assignment is emitted in the constructor body anyway.
We just used it for default value of the corresponding parameter. The important thing is, in case of a reference type it will be translated to a
null
default and a null coalescing operator.Because these properties should have a
default
value, using #7737 and #206 you will have,And additional constructor parameters will be generated along with the record expansion.
Related: #229
The text was updated successfully, but these errors were encountered: