Replies: 4 comments
-
I know that the team is aware of this but I want to make sure that it's a part of the discussion. There would be concerns as to how these initializers would interact with overloads. To maintain compatibility with existing code the compiler would be forced to choose an overload where the parameter is type class Person {
public string First { get; set; }
public string Last { get; set; }
}
void M(object o) => Console.WriteLine("object");
void M(Person p) => Console.WriteLine("person");
void Test() {
M(new { First = "Matt", Last = "Warren" }); // prints object
M(new() { First = "Matt", Last = "Warren" }); // prints Person
} This could be considered pathological, but also: class Foo {
public void M(Person p) => Console.WriteLine("Person");
}
static class FooExtensions {
public static void M(this Foo foo, object o) => Console.WriteLine("object");
}
void Test() {
var foo = new Foo();
foo.M(new { First = "Matt", Last = "Warren" }); // prints object, resolves to extension method
foo.M(new() { First = "Matt", Last = "Warren" }); // prints Person, resolves to instance method
} |
Beta Was this translation helpful? Give feedback.
-
This practically adds a conversion to For instance, |
Beta Was this translation helpful? Give feedback.
-
Its ambiguous if there is no tie breaker. |
Beta Was this translation helpful? Give feedback.
-
Well if that's the only thing changing the meaning of If that's the route we're taking, I think it can be applied to other features as well, such as #2926 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Target Typed Anonymous Type Initializers
Summary
Allow target typing to be used to override the meaning of anonymous type initializers, so the typed being constructed is the target type and not a compiler defined anonymous type.
Motivation
Target typing already exists and can be used to construct type instances without specifying the type name by using the target typed new construction syntax. This includes (in addition to normal position arguments) object initializer syntax.
For types without explicit constructors or types with no-argument constructors, this kind of construction syntax becomes almost identical to anonymous type construction/initializer syntax.
There is an unnecessary and subtle distinction between the two. The anonymous type initializer is allowed to construct a type on the fly and the other is required to infer the type from context. The only syntactic difference being the existence of a pair of parentheses.
We can unify these two disparate constructs by allowing the anonymous type initializer syntax to be use with target typing, and instead of inventing a new type simply using the one we already know.
Detailed design
TBD
Drawbacks
Alternatives
Design Meetings
https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-10-26.md#target-typed-anonymous-type-initializers
Beta Was this translation helpful? Give feedback.
All reactions