-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion "Target-typed new
expression" (VS 16.8, .NET 5)
#100
Comments
new
expressionnew
expression
At least this can be done with object initializers without type-name, which currently generate anonymous types. |
new
expressionnew
expression"
I am more fond of |
@Thaina I absolutely agree for locals, but for fields and properties this would actually be preferable to me and a_m_a_z_i_n_g, not gonna lie. private readonly Dictionary<(Foo foo, Bar bar), List<Baz>> bazByFooAndBar = new(); Love it! Plus also: private readonly Dictionary<Foo, Bar> barByFoo;
public ctor(IDictionary<Foo, Bar> initialValues)
{
barByFoo = new(initialValues);
} I do a lot of field initialization requiring access to |
@jnm2 Well, readonly in constructor seem great But still that the only place I think we should do like that, I don't like mutable reference so field should not new anywhere. I wish we could have |
I believe this is a limitation of the fact that C# has a two-pass compiler. The entire signature of a class needs to be known before any of the initialization expressions can be executed, so if you use var for a field the compiler doesn't know the type on the right-hand side of the |
I think private |
@alrz A In any case, that would be a completely separate language proposal. |
Yes please, let's make things less verbose whenever possible and whenever it make sense. :) |
I'm not sure is it related with this topic.
This piece of code could be, with some generalization, written as
I really don't know is it readable and right direction. Possibly not. Just to leave a little note. |
I'm a bit riven by this. On one hand I think that a method's body should be independent of its signature, meaning that the creation of an object for a |
Constructors are kind of useless at the moment, you can't do type inference with them, can't pass a bound constructor as a first class function, etc. etc. At the same time you're stuck using them if you want to use It would be great if there could be some focus on bringing constructor functions into parity with plain old static methods, instead of piecemeal workarounds like this. |
Reading the raw notes:
Looking at the concern that an API adding a new overload could break consumers, the "simple" subset of places where target-typed
The first two are the places where it seems like most people are really clamoring for it; the last one I just came up at the spur of the moment. |
Even if it's not ambiguous to the compiler it's still ambiguous and confusing to the reader. I would find it to be very strange if a target-typed new expression suddenly turned into an anonymous type expression by introducing an overload. |
I agree with that. Personally, I would be fine if target-typed new was only supported for initializing fields and properties, which is the only case where you type the same type name twice in a row. I’d be fine with requiring the parentheses. But I think I’ve shown that it’s a small edge case where the compiler could not tell that leaving out the parentheses was a grammatical error for target-typed new and not an illegal cast from an anonymous type. I think it’s reasonable to ask for an improved error message, which was my original point. |
Definitely. However, improved error messages should be requested at dotnet/Roslyn, not csharplang. Thanks! |
Playing with the preview, I've really enjoyed this feature, thank you. However, I've missed it when initializing arrays. Are there plans to support target typed new for array types? I realize I can use Implicitly Typed Array syntax, but this won't work when initializing an array with no initial elements, even if there is an explicit declaration of the target variable type. It would be useful to have the ability to initialize them as such: public (float, int, string)[] foo = new [2]; |
It does not appear that there is, from the proposed spec:
Arrays are also listed in the 2018-06-25 LDM meeting notes as an unconstructible type. I agree it seems like arrays should be supported. Based on the wording from the spec, I think it'd be considered to be its own feature separate from C# 9's target-typed new. |
Ah, I missed that document. Thank you for providing the info. I suppose that's that. Hopefully in the future 🤞 |
We discussed target-typing for array creation, but in many useful cases it is not trivial to figure out the target element type. For instance |
@jcouv If I understand that correctly that's for having |
@jcouv, understood, looks like there's some design decisions that need to be settled first. Thanks for the example and the link to the proposal.
@lcsondes it looks like both are covered in the linked proposal:
|
new
expression"new
expression" (VS 16.8, .NET 5)
What's the plan for |
See #2701. |
Thank you, but that only seems to talk about target-typed expressions between the {}. Right now I think the compiler is expecting a Is this secretly covered? It would be nice if we could get an explicit yes/no. |
I'm not sure what you mean by 'secretly covered' |
That I do not see a number within square brackets in any of the proposed target-typed new array examples. |
That is the proposal I just linked. |
All I'm trying to get is an explicit yes/no on whether that one covers |
I think it's something LDM could naturally include alongside that proposal, but unless I'm blind nothing in the #2701 suggests that it would enable something like |
We can't give an explicit yes/no since it hasn't been designed yet. I can say that it would definitely be part of the discussion. However, there's no guarantees that can be given for a future feature that hasn't even been brought to ldm to do. |
@CyrusNajmabadi why is this issue still open and with pending tasks? Hasn't this already shipped some time ago? Apologies if I'm missing something obvious, but this is not making much sense to me. |
Issues are left open until the relevant specs and documentation are fully updated. |
That's what the |
Summary: Allow
Point p = new (x, y);
Shipped in preview in 16.7p1.
See also dotnet/roslyn#35
Test plan: dotnet/roslyn#28489
The text was updated successfully, but these errors were encountered: