Improved type inference with arrays and the new() syntax. #7175
-
When constructing an object graph, it seems out of place not to be able to use new() with type inference when constructing arrays. Is there a reason the type information cannot be inferred from the left side? I have large object graphs without any type names appearing, apart from this one exception (surely it would be reasonable to improve the developer experience). Here are examples that all work, apart from the last one. Shouldn't the compiler be able to infer the items on the right are of type Student? (see the line producing an error with the comment at the end): Student student = new("John");
string[] names = new[] { "John", "Jane" };
Student[] studentsA = new[] { new Student("John"), new Student("Jane") }; }
Student[] studentsB = new[] { new("John"), new("Jane") }; // Compile error (cannot infer)
record Student(string Name); Could this be improved in the next version? Also, please make sure any improvements flow through to the new literal syntax shown below (targeted for C# 12?): Student[] students = [new("John"),new("Jane")]; Thanks you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I am closing given there is an open issue tracking this (link above) |
Beta Was this translation helpful? Give feedback.
csharplang/meetings/2020/LDM-2020-09-09.md
Line 163 in af3e3b4