Championed: Target-typed implicit array creation expression new[]
#8992
Replies: 15 comments
This comment has been hidden.
This comment has been hidden.
-
Would it not work better to relax the array initializer syntax to allow |
Beta Was this translation helpful? Give feedback.
-
I would like if this also included |
Beta Was this translation helpful? Give feedback.
-
I think this proposal can effectively be subsumed into collection literals. You would be able to do the following: KeyValuePair<string, string>[] array = [new("key1", value1), new("key2", value2)]; That solves the array part. KVP is also very special to collection literals so the above could also be shortened to: KeyValuePair<string, string>[] array = ["key1": value1, "key2": value2];
We support collection/dictionary literals even the absence of a concrete collection type :) |
Beta Was this translation helpful? Give feedback.
-
Right, I think it will also work with IEnumerable but only as long as there is a compatible natural type. IEnumerable<T> x = [value]; // ok; but the target-type (T) won't affect type inference in any ways here.
IEnumerable<T> x = []; // error; no natural type? (is there a default natural type when the list is empty?)
IEnumerable<bool?> x = [true, null]; // error; the most-common-type is `bool` and `List<bool>` fails to assign. Relates to dotnet/roslyn#37950 |
Beta Was this translation helpful? Give feedback.
-
All of the above would be legal @alrz |
Beta Was this translation helpful? Give feedback.
-
Great! Though I didn't find anything about a null-aware common type.. does that last example work with |
Beta Was this translation helpful? Give feedback.
-
The last example will work with 'var' if 'best common type' is willing to infer |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, I think it is not. SharpLab |
Beta Was this translation helpful? Give feedback.
-
@RikkiGibson right, that's what i'm saying. We're gated on that being a supported concept for c#. the same applies to existing collections today. |
Beta Was this translation helpful? Give feedback.
-
When we didn't do that in C# 8, we accepted that it would likely never be done. #881 |
Beta Was this translation helpful? Give feedback.
-
I opened a discussion thread here: #7175 In short: It is great if this type of syntax will work in C# 12: Student[] students = [new("John"),new("Jane")]; But we still should have a goal of implementing this feature on the original syntax for the sake of language consistency. Student[] studentsB = new[] { new("John"), new("Jane") }; // This would ideally work as well. |
Beta Was this translation helpful? Give feedback.
-
I don't believe we need this. You can already write:
Spending any effort here would be risky (as it now involves target typing in a scenario that normally uses best-common-typing, and it is subsumed by an existing feature. Any work here would take away from new feature work for areas that would provide much more value. Given that code can move from |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi *I take it back. Dictionary doesn't seem to be covered, either. |
Beta Was this translation helpful? Give feedback.
-
That's an entirely different proposal. Implicit array creation has never supported supplying a length. This request is for target typing the elements. It would not subsume this use case |
Beta Was this translation helpful? Give feedback.
-
Allow
KeyValuePair<string, string>[] array = new[] { new("key1", value1), new("key2", value2) };
Champion issue: #8993
With introduction of
new()
expression (#100), the following:can be simplified to:
but you still need to repeat the type following the field/property initializer. Closest you can get is something like:
For the sake of completeness, I'd suggest to also make
new[]
a target-typed expression,The mechanics of such feature is similar to #2389 or #2460 where there are two source of type inference, the common type and the target-type.
Note: A list/dictionary literal feature would not supersede this proposal because in above examples there's no concrete collection type to infer.
Beta Was this translation helpful? Give feedback.
All reactions