-
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] Object initializers for factory methods #133
Comments
The con could be a big con, but it could be a pro if you expand the scope of the idea. Setting multiple properties on an object after it's been created is not an unheard of thing. |
Can you give an example of what syntax ambiguities does the use of Can you elaborate on the cons part? Are you aware that this:
is translated into this?
The use cases for a block assignment are many. One is the assign properties of return values of methods. But I could also do something like this:
I just think you got the motivation all wrong. |
I'm not sure if there are any in the spec at the moment, but allowing
E.g. var x = new X();
var y = GetSomething(1, 2, x with { A = 5 }, 4, 5);
// x here has A = 5, which is not immediately obvious I suppose it can be avoided if initializers are limited to method call results only.
Yes, however I don't want to write it myself for factory methods, given that translator is already in the language.
Can you elaborate? Is your motivation mostly about allowing modification of existing objects? |
@ashmind, I still find the whole thing lacks consistency. You talk about side effects when there aren't any, in comparison. Sure shorthand would be nice. But that's just it. There are no other pros or cons involved. |
The proposal also solves the problem of object initializers in Syntax can be similar to the existing (compare using (var x = new X()) {
x = {
A = 1,
B = 2,
C = { D = { 3, 4 } },
};
} |
IMHO
and this lacks some sort of readability; So far the original idea from @ashmind is the best solution for the questions listed above. |
It's desirable to allow the same initializers on factories as constructor calls. We need to think this through in conjunction with |
i agree that the 'with' keyword seems unnecessary. i'd like to see this simple extension of 'object initializers' to arbitrary expressions:
|
Issue moved to dotnet/csharplang #803 via ZenHub |
Problem
This is another one of the issues when a change to logic significantly changes code structure.
Let's say I had the following:
Now I decided all
X
should be created through anXFactory
instead.So I get:
One small logical change causes a total restructure of not-really-related code.
Potential solution
Provide a way to use object initializer with any existing object.
Example:
Keyword
with
here is provided to remove any syntax ambiguities.Pros
Cons
The text was updated successfully, but these errors were encountered: