Instance Blocks #1652
Replies: 11 comments
-
That be very nice for factory methods where you cannot use the initializer blocks, I would use a comma as separator though. return CreateEmployee()
.{
Name = "John",
Age = 43
}; |
Beta Was this translation helpful? Give feedback.
-
The advantage of the semi-colons is that there is no reason to restrict what kind of code can be included inside the braces. It could function as a normal code block - with additional features. |
Beta Was this translation helpful? Give feedback.
-
This is a slightly alternative syntax to that proposed in #803. |
Beta Was this translation helpful? Give feedback.
-
I think this is different enough from #803 to warrant a separate proposal. This one is broader, providing a block in which all visible members of the object are brought into scope. #803 (and the comment by @quinmars) are solely targeted at setting properties or fields on the object. |
Beta Was this translation helpful? Give feedback.
-
This proposal needs more detail, such as how scoping works. Presumably conflicting names would be resolved to the object's members. public class A
{
public string S => "S in A";
}
public class B
{
string S => "S in B";
private void MyMethod()
{
string S = "S local";
new A()
.{
Console.WriteLine(S); // "S in A"
Console.WriteLine(this.S); // "S in B"
};
}
} There would be no way to access the local |
Beta Was this translation helpful? Give feedback.
-
I don't have a solution to that problem, but an off-the-cuff idea is to use a keyword |
Beta Was this translation helpful? Give feedback.
-
Even with string S = "S local";
new A()
.{
new A()
.{
// Local A.S is accessible via 'local S'
// Inner A.S is accessible via 'S'
// Outer A.S is inaccessible
};
}; In most other situations this is avoided by not allowing a new name to shadow existing names. But here it is an unavoidable issue as the inner scope is formed from existing member names. We can't say you aren't allowed to shadow, since the type is defined externally and possibly by a third party. |
Beta Was this translation helpful? Give feedback.
-
Just to be clear about the suggestion, using your example it would be:
Whatever problems there are with this approach (and I'm not suggesting it is the solution), it does make the |
Beta Was this translation helpful? Give feedback.
-
@blair-ahlquist Notice in my last example that the |
Beta Was this translation helpful? Give feedback.
-
I totally missed that. Good example. |
Beta Was this translation helpful? Give feedback.
-
The idea is nice - but I dislike the syntax with the dot. In addition I dislike the semicolon - we should not allow arbitrary statements in such a block / initializer. var newFoo = SomeFactoryClass.CreateFoo(){
AFooProp = "Bar",
Bar = 4711
}; It could be lowered simply to: var newFoo = SomeFactoryClass.CreateFoo();
newFoo.AFooProp = "Bar";
newFoo.Bar= 4711; And for collection initializers, it could be translated to var newFoo = SomeFactoryClass.CreatePrefilledList(){
1, 2, 3
}; -> var newFoo = SomeFactoryClass.CreatePrefilledList();
newFoo.Add(1);
newFoo.Add(2);
newFoo.Add(3); |
Beta Was this translation helpful? Give feedback.
-
Whenever curly braces are used directly after an object, that object's properties and methods can be referred to directly. The return value for the resulting expression is the original object. It wouldn't be required to come directly after a constructor.
Example:
I set a property, registered for an event, and called a method all within the curly braces. The return value for the expression was then set to
myC
. This feature would largely supercede initializers.Beta Was this translation helpful? Give feedback.
All reactions