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

Update target-typed-new.md #1989

Merged
merged 5 commits into from
Mar 19, 2020
Merged

Update target-typed-new.md #1989

merged 5 commits into from
Mar 19, 2020

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Nov 7, 2018

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.

@jcouv
Copy link
Member Author

jcouv commented Nov 8, 2018

Hum, I forgot that @alrz had already queued a PR to update the spec significantly. Let's get that one in first: #1780

Update: that's done (conflicts resolved).

@jcouv jcouv added the Blocked Waiting for a dependency label Nov 8, 2018
@jcouv jcouv removed the Blocked Waiting for a dependency label Nov 8, 2018

> **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.
Copy link
Contributor

@alrz alrz Nov 8, 2018

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, ...
Copy link
Contributor

@alrz alrz Nov 8, 2018

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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().

@jcouv
Copy link
Member Author

jcouv commented Dec 17, 2019

Some notes from reviewing dotnet/roslyn#39658

  • the conversion is called "object creation" conversion
  • rules for new() and validity of object equality comparisons
  • the type of new() is the stripped type for nullable types

@jcouv jcouv merged commit a6c3852 into master Mar 19, 2020
@jcouv jcouv deleted the dev/jcouv/target-typed-new branch March 19, 2020 18:44

`throw new()` is disallowed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 It is now allowed.

Copy link
Member Author

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.
Copy link
Contributor

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 :)

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

Successfully merging this pull request may close these issues.

3 participants