Skip to content

Commit 0c484ac

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 1477dfc commit 0c484ac

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

standard/conversions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ While throw expressions do not have a type, they may be implicitly converted to
384384
385385
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`.
386386
387+
### §imp-obj-creation-conv Implicit object-creation conversions
388+
389+
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.
390+
391+
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.
392+
387393
## 10.3 Explicit conversions
388394
389395
### 10.3.1 General

standard/expressions.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,11 @@ An *object_creation_expression* is used to create a new instance of a *class_typ
25482548
object_creation_expression
25492549
: 'new' type '(' argument_list? ')' object_or_collection_initializer?
25502550
| 'new' type object_or_collection_initializer
2551+
| target_typed_new
2552+
;
2553+
2554+
target_typed_new
2555+
: 'new' '(' argument_list? ')' object_or_collection_initializer?
25512556
;
25522557
25532558
object_or_collection_initializer
@@ -2558,6 +2563,10 @@ object_or_collection_initializer
25582563

25592564
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*.
25602565

2566+
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.
2567+
2568+
If `type` is present, let `T` be that type; otherwise, let `T` be the implied type.
2569+
25612570
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*.
25622571

25632572
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.
@@ -2566,7 +2575,7 @@ Processing of an object creation expression that includes an object initializer
25662575

25672576
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).
25682577

2569-
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:
2578+
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:
25702579

25712580
- If `T` is a *value_type* and `A` is not present:
25722581
- 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).
@@ -2581,7 +2590,7 @@ The binding-time processing of an *object_creation_expression* of the form `new
25812590

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

2584-
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:
2593+
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:
25852594

25862595
- If `T` is a *class_type*:
25872596
- 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
@@ -1656,7 +1656,7 @@ throw_statement
16561656
;
16571657
```
16581658
1659-
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.
1659+
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.
16601660
16611661
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.
16621662

0 commit comments

Comments
 (0)