Add support for target typed new expressions#990
Closed
RexJaeschke wants to merge 8 commits intodotnet:draft-v9from
Closed
Add support for target typed new expressions#990RexJaeschke wants to merge 8 commits intodotnet:draft-v9from
RexJaeschke wants to merge 8 commits intodotnet:draft-v9from
Conversation
|
|
||
| There is an implicit ***object-creation conversion*** from a *target_typed_new* expression ([§12.8.16.2](expressions.md#128162-object-creation-expressions)) to every type. | ||
|
|
||
| Given a target type `T`, if `T` is an instance of `System.Nullable`, the type `T0` is `T`'s underlying type. Otherwise `T0` is `T`. The meaning of a *target_typed_new* expression that is converted to the type `T` is the same as the meaning of a corresponding *object_creation_expression* that specifies `T0` as the type. |
Contributor
There was a problem hiding this comment.
System.Nullable is a type but not a generic type. How is this worded elsewhere — System.Nullable<> or System.Nullable<T>?
| @@ -2296,6 +2301,10 @@ object_or_collection_initializer | |||
|
|
|||
| The *type* of an *object_creation_expression* shall be a *class_type*, a *value_type*, or a *type_parameter*. The *type* cannot be a *tuple_type* or an abstract or static *class_type*. | |||
Contributor
There was a problem hiding this comment.
Ok I suppose this disallows dynamic and pointer types as type even in target-typed new.
|
|
||
| The *type* of an *object_creation_expression* shall be a *class_type*, a *value_type*, or a *type_parameter*. The *type* cannot be a *tuple_type* or an abstract or static *class_type*. | ||
|
|
||
| If `type` can be inferred from usage, it can be omitted, as allowed by *target_typed_new*. It is a compile-time error to omit `type` if the type cannot be inferred. A *target_typed_new* expression has no type. However, there is an implicit object-creation conversion (§imp-obj-creation-conv) from a *target_typed_new* expression to every type. It is a compile-time error if a *target_typed_new* is used as an operand of a unary or binary operator, or if it is used where it is not subject to an object-creation conversion. |
Contributor
There was a problem hiding this comment.
Together with C# 9 target-typed conditional expressions, this will then allow
class C {
char M() {
return new() ? new() : new();
}
}in which the target types will be inferred as
class C {
char M() {
return new bool() ? new char() : new char();
}
}It might be clearest to merge that feature before this one, so that there is no doubt about why target-typed new is allowed in operands of the ternary operator.
Member
|
Closing in favor of #1469 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The MS proposal contains a "Miscellaneous" section, which contains a list of "consequences of the specification." These have not been added to this PR! Someone should verify that these are covered sufficiently by normative text, and even if they all are, it might be worth adding some Notes to that effect.