-
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
Update target-typed-new.md #1989
Conversation
proposals/target-typed-new.md
Outdated
|
||
> **Open Issue:** should we allow `throw new()` with `Exception` as the target-type? | ||
Target-typed `new` is allowed with binary operators, where the target type is determined by overload resolution. This excludes tuple equality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This excludes tuple equality.
Does this mean we neither allow new() != (1, 2)
nor (new(), 0) != (1, 2)
?
|
||
We have `throw null` today, but not `throw default` (though it would have the same effect). On the other hand, `throw new()` could be actually useful as a shorthand for `throw new Exception(...)`. Note that it is already allowed by the current specification. `Exception` is a reference type, and the specification for the throw statement says that the expression is converted to `Exception`. | ||
It is disallowed when there is no type to target: unary operators, collection of a `foreach`, in a `using`, in a deconstruction, in an `await` expression, as an anonymous type property (`new { Prop = new() }`), in a `lock` statement, in a `sizeof`, in a `fixed` statement, in a member access (`new().field`), in a dynamically dispatched operation (`someDynamic.Method(new())`), in a LINQ query, as the operand of the `is` operator, as the left operand of the `??` operator, ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in a LINQ query,
Actually we permit that - per dotnet/roslyn#25196 (comment). The resulting expression tree would be unaffected vs. using the explicit type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include goto case new()
here? to my surprise, goto case default
works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goto case
has a target type (the type of the switch expression). In this case it doesn't work because it isn't a constant expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify my point, it's now forbidden to write case default:
to avoid confusion with default:
case. Same reasoning could be applied to goto default;
and goto case default;
.
it doesn't work because it isn't a constant expression.
It could work because we now permit new()
with primitive types, per ldm.
Anywho, I think we should permit goto case new()
iff we permit case new()
.
Some notes from reviewing dotnet/roslyn#39658
|
|
||
`throw new()` is disallowed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 It is now allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
Allow omitting the type when it can be inferred from usage. | ||
```cs | ||
XmlReader.Create(reader, new() { IgnoreWhitespace = true }); | ||
``` | ||
|
||
Instantiate an object without spelling out the type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Probably just remove all examples, I realized they are basically the same thing :)
Tagging @alrz and @gafter for review. Feel free to make additional changes.
In reviewing the LDM history, I noticed that some notes say that
new { property = 1 }
should work (the constructor arguments are optional).See https:/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-06-25.md#syntax
But thinking more about it, I don't think that's possible, as that syntax is ambiguous with an anonymous type creation.