Declarations within object initialisers #400
Replies: 8 comments
-
The need to reuse an object instance in object initializers has led to many a refactor away from object initializers. I would use this somewhat frequently. |
Beta Was this translation helpful? Give feedback.
-
Looks like this proposal is to allow for declaration/assignments to be used as expressions within collection initializers. The result of the expression is that of the assignment and that would be added to the collection. I think it would be weird to support declaration/assignments but not just assignments, and just assignments wouldn't be possible since it would be ambiguous with object initializer syntax. Are you asking about some way to declare temporary variables within an object initializer? That's not what this proposal seems to be about. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Thanks, you're right. I saw what I wanted to see. Back to what the proposal is really about, we already have this which I use: var simulation = new Simulation();
{
var pointMass1 = new PointMass(mass: 1.0f);
var pointMass2 = new PointMass(mass: 2.0f);
simulation.Add(new Spring(pointMass1, pointMass2));
}; I'd love to be able to do this other thing too: var simulation = new Simulation
{
new Spring(
var pointMass1 = new PointMass(mass: 1.0f),
var pointMass2 = new PointMass(mass: 2.0f)),
new Spring(
pointMass2,
pointMass1)
}; |
Beta Was this translation helpful? Give feedback.
-
Close. Look at the original code he posted. He wants the result of the declaration assignment to also be added to the collection: var simulation = new Simulation();
{
var pointMass1 = new PointMass(mass: 1.0f);
var pointMass2 = new PointMass(mass: 2.0f);
simulation.Add(pointMass1);
simulation.Add(pointMass2);
simulation.Add(new Spring(pointMass1, pointMass2);
}; |
Beta Was this translation helpful? Give feedback.
-
Well, I guess we are asking for the same thing then, if it looks like this: var simulation = new Simulation
{
var pointMass1 = new PointMass(mass: 1.0f),
var pointMass2 = new PointMass(mass: 2.0f),
new Spring(pointMass1, pointMass2)
}; |
Beta Was this translation helpful? Give feedback.
-
Since // version that's been possible for some time:
var pointMass1 = new PointMass(mass: 1.0f);
var pointMass2 = new PointMass(mass: 2.0f);
var simulation = new Simulation
{
pointMass1,
pointMass2,
new Spring(pointMass1, pointMass2)
};
// C# 7 version:
var simulation = new Simulation
{
new PointMass(mass: 1.0f) is var pointMass1 ? pointMass1 : pointMass1,
new PointMass(mass: 1.0f) is var pointMass2 ? pointMass2 : pointMass2,
new Spring(pointMass1, pointMass2)
}; I've found the expression
So the example code would then be: var simulation = new Simulation
{
new PointMass(mass: 1.0f) as var pointMass1,
new PointMass(mass: 1.0f) as var pointMass2,
new Spring(pointMass1, pointMass2)
}; |
Beta Was this translation helpful? Give feedback.
-
I had an extraneous Another way of asking this question would be: why can't variable declarations with immediate assignments be treated as expressions in the general case? As @jnm posted, why couldn't the following work? var spring = new Spring(
var pointMass1 = new PointMass(mass: 1.0f),
var pointMass2 = new PointMass(mass: 2.0f)); A few years ago this would have looked horrible, but now with C# 7 showing us that declarations work within expressions without the sky falling down, it seems there could be more opportunity here -- at least from the outside looking in. |
Beta Was this translation helpful? Give feedback.
-
I had the idea for a slight variation on @DavidArno's new Table
{
Columns =
{
new Column { Name = "Id" } idColumn // 😈
},
PrimaryKey = new PrimaryKey
{
Columns = { idColumn }
}
}; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have some code:
Now that I've been using C# 7 for a while and am starting to get used to declarations in new places, it feels like the following would be a clearer form of the above:
Edit Fixed the expected output code
Beta Was this translation helpful? Give feedback.
All reactions