Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Championed: Target-typed implicit array creation expression new[] #2701

Open
1 of 5 tasks
alrz opened this issue Jul 30, 2019 · 12 comments
Open
1 of 5 tasks

Championed: Target-typed implicit array creation expression new[] #2701

alrz opened this issue Jul 30, 2019 · 12 comments

Comments

@alrz
Copy link
Contributor

alrz commented Jul 30, 2019

Allow KeyValuePair<string, string>[] array = new[] { new("key1", value1), new("key2", value2) };

  • Proposal added (below)
  • Discussed in LDM
  • Decision in LDM
  • Finalized (done, rejected, inactive)
  • Spec'ed

With introduction of new() expression (#100), the following:

IEnumerable<KeyValuePair<string, string>> Headers = new[]
{
     new KeyValuePair<string, string>("Foo", foo),
     new KeyValuePair<string, string>("Bar", bar),
}

can be simplified to:

IEnumerable<KeyValuePair<string, string>> Headers = new KeyValuePair<string, string>[]
{
     new("Foo", foo),
     new("Bar", bar),
}

but you still need to repeat the type following the field/property initializer. Closest you can get is something like:

IEnumerable<KeyValuePair<string, string>> Headers = new[]
{
     new KeyValuePair<string, string>("Foo", foo),
     new("Bar", bar),
}

For the sake of completeness, I'd suggest to also make new[] a target-typed expression,

IEnumerable<KeyValuePair<string, string>> Headers = new[]
{
     new("Foo", foo),
     new("Bar", bar),
}

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.

@jcouv jcouv self-assigned this Sep 17, 2019
@jcouv jcouv changed the title Discussion: Target-typed implicit array creation expression new[] Championed: Target-typed implicit array creation expression new[] Sep 18, 2019
@alrz

This comment has been minimized.

@gafter gafter added this to the 9.0 candidate milestone Oct 1, 2019
@lcsondes
Copy link

Would it not work better to relax the array initializer syntax to allow {{a,b},{c,d}} instead of {new(a,b),new(c,d)} for the KeyValuePair example?

@lcsondes
Copy link

lcsondes commented Jul 9, 2021

I would like if this also included int[] x = new[1];, not just new[]{...}.

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Oct 24, 2022

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];

Note: A list/dictionary literal feature would not supersede this proposal because in above examples there's no concrete collection type to infer.

We support collection/dictionary literals even the absence of a concrete collection type :)

@alrz
Copy link
Contributor Author

alrz commented Oct 25, 2022

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

@CyrusNajmabadi
Copy link
Member

All of the above would be legal @alrz

@alrz
Copy link
Contributor Author

alrz commented Oct 25, 2022

Great! Though I didn't find anything about a null-aware common type.. does that last example work with var?

@CyrusNajmabadi
Copy link
Member

The last example will work with 'var' if 'best common type' is willing to infer bool? for true and null. So we would be gated on that.

@RikkiGibson
Copy link
Contributor

if 'best common type' is willing to infer bool? for true and null.

Unfortunately, I think it is not. SharpLab

@CyrusNajmabadi
Copy link
Member

@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.

@333fred
Copy link
Member

333fred commented Oct 25, 2022

When we didn't do that in C# 8, we accepted that it would likely never be done. #881

@audacode
Copy link

audacode commented May 8, 2023

I opened a discussion thread here: #7175
But closed it once I found this issue..

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants