Skip to content

Commit 3b403b9

Browse files
RexJaeschkeBillWagner
authored andcommitted
Add support for target-typed new expressions
Add support for target-typed new expressions Add support for target-typed new expressions fix link fix link fix warnings
1 parent 2f53536 commit 3b403b9

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

standard/conversions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ While throw expressions do not have a type, they may be implicitly converted to
390390
391391
There is an implicit conversion from a *switch_expression* ([§12.11](expressions.md#1211-switch-expression)) to every type `T` for which there exists an implicit conversion from each *switch_expression_arm*s *switch_expression_arm_expression*s to `T`.
392392
393+
### §imp-obj-creation-conv Implicit object-creation conversions
394+
395+
There is an implicit ***object-creation conversion*** from a *target_typed_new* expression ([§12.8.17.2](expressions.md#128172-object-creation-expressions)) to every type.
396+
397+
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.
398+
393399
## 10.3 Explicit conversions
394400
395401
### 10.3.1 General

standard/expressions.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,11 @@ An *object_creation_expression* is used to create a new instance of a *class_typ
25232523
object_creation_expression
25242524
: 'new' type '(' argument_list? ')' object_or_collection_initializer?
25252525
| 'new' type object_or_collection_initializer
2526+
| target_typed_new
2527+
;
2528+
2529+
target_typed_new
2530+
: 'new' '(' argument_list? ')' object_or_collection_initializer?
25262531
;
25272532
25282533
object_or_collection_initializer
@@ -2533,6 +2538,10 @@ object_or_collection_initializer
25332538

25342539
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*.
25352540

2541+
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.
2542+
2543+
If `type` is present, let `T` be that type; otherwise, let `T` be the implied type.
2544+
25362545
The optional *argument_list* ([§12.6.2](expressions.md#1262-argument-lists)) is permitted only if the *type* is a *class_type* or a *struct_type*.
25372546

25382547
An object creation expression can omit the constructor argument list and enclosing parentheses provided it includes an object initializer or collection initializer. Omitting the constructor argument list and enclosing parentheses is equivalent to specifying an empty argument list.
@@ -2541,7 +2550,7 @@ Processing of an object creation expression that includes an object initializer
25412550

25422551
If any of the arguments in the optional *argument_list* has the compile-time type `dynamic` then the *object_creation_expression* is dynamically bound ([§12.3.3](expressions.md#1233-dynamic-binding)) and the following rules are applied at run-time using the run-time type of those arguments of the *argument_list* that have the compile-time type `dynamic`. However, the object creation undergoes a limited compile-time check as described in [§12.6.5](expressions.md#1265-compile-time-checking-of-dynamic-member-invocation).
25432552

2544-
The binding-time processing of an *object_creation_expression* of the form `new T(A)`, where `T` is a *class_type*, or a *value_type*, and `A` is an optional *argument_list*, consists of the following steps:
2553+
The binding-time processing of an *object_creation_expression* of the form `new T(A)`, where the specified or implied type `T` is a *class_type*, or a *value_type*, and `A` is an optional *argument_list*, consists of the following steps:
25452554

25462555
- If `T` is a *value_type* and `A` is not present:
25472556
- The *object_creation_expression* is a default constructor invocation. The result of the *object_creation_expression* is a value of type `T`, namely the default value for `T` as defined in [§8.3.3](types.md#833-default-constructors).
@@ -2556,7 +2565,7 @@ The binding-time processing of an *object_creation_expression* of the form `new
25562565

25572566
Even if the *object_creation_expression* is dynamically bound, the compile-time type is still `T`.
25582567

2559-
The run-time processing of an *object_creation_expression* of the form new `T(A)`, where `T` is *class_type* or a *struct_type* and `A` is an optional *argument_list*, consists of the following steps:
2568+
The run-time processing of an *object_creation_expression* of the form `new T(A)`, where the specified or implied type `T` is *class_type* or a *struct_type* and `A` is an optional *argument_list*, consists of the following steps:
25602569

25612570
- If `T` is a *class_type*:
25622571
- A new instance of class `T` is allocated. If there is not enough memory available to allocate the new instance, a `System.OutOfMemoryException` is thrown and no further steps are executed.

standard/statements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ throw_statement
16551655
;
16561656
```
16571657
1658-
A `throw` statement with an expression throws an exception produced by evaluating the expression. The expression shall be implicitly convertible to `System.Exception`, and the result of evaluating the expression is converted to `System.Exception` before being thrown. If the result of the conversion is `null`, a `System.NullReferenceException` is thrown instead.
1658+
A `throw` statement with an expression throws an exception produced by evaluating the expression. The expression shall be implicitly convertible to `System.Exception`, and the result of evaluating the expression is converted to `System.Exception` before being thrown. If *expression* is a *target_typed_new* expression ([§12.8.17.2](expressions.md#128172-object-creation-expressions)), the target type is `System.Exception`. If the result of the conversion is `null`, a `System.NullReferenceException` is thrown instead.
16591659
16601660
A `throw` statement with no expression can be used only in a `catch` block, in which case, that statement re-throws the exception that is currently being handled by that `catch` block.
16611661

0 commit comments

Comments
 (0)