-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add support for target typed new expressions #990
base: draft-v9
Are you sure you want to change the base?
Add support for target typed new expressions #990
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. |
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.
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*. |
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.
Ok I suppose this disallows dynamic
and pointer types as type even in target-typed new.
@@ -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*. | |||
|
|||
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. |
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.
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.
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.