Skip to content

Commit bcda781

Browse files
committed
Apply delta from PR #1561 (Parameterless struct constructors) - 9 new review commits
1 parent 5bca467 commit bcda781

3 files changed

Lines changed: 6 additions & 5 deletions

File tree

standard/expressions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2673,7 +2673,8 @@ If any of the arguments in the optional *argument_list* has the compile-time typ
26732673
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:
26742674

26752675
- If `T` is a *value_type* and `A` is not present:
2676-
- 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).
2676+
- If `T` is a *struct_type* that has an explicitly declared parameterless instance constructor, that constructor is a candidate and is selected via overload resolution as described below.
2677+
- Otherwise, 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).
26772678
- Otherwise, if `T` is a *type_parameter* and `A` is not present:
26782679
- If no value type constraint or constructor constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)) has been specified for `T`, a binding-time error occurs.
26792680
- The result of the *object_creation_expression* is a value of the run-time type that the type parameter has been bound to, namely the result of invoking the default constructor of that type. The run-time type may be a reference type or a value type.

standard/structs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ As described in [§9.3](variables.md#93-default-values), several kinds of variab
573573
>
574574
> *end example*
575575
576-
The default value of a struct corresponds to the value returned by the default constructor of the struct ([§8.3.3](types.md#833-default-constructors)). Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor, which always returns the value that results from setting all fields to their default values.
576+
The default value of a struct corresponds to the value returned by the default constructor of the struct ([§8.3.3](types.md#833-default-constructors)). When a struct does not declare an explicit parameterless instance constructor, the default constructor is synthesized and always returns the value that results from setting all fields to their default values. The `default` expression always produces the zero-initialized default value, even when a struct declares an explicit parameterless instance constructor ([§16.4.9](structs.md#1649-constructors)).
577577
578578
> *Note*: Structs should be designed to consider the default initialization state a valid state. In the example
579579
>

standard/types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Note that `System.ValueType` is not itself a *value_type*. Rather, it is a *clas
231231

232232
### 8.3.3 Default constructors
233233

234-
All value types implicitly declare a public parameterless instance constructor called the ***default constructor***. The default constructor returns a zero-initialized instance known as the ***default value*** for the value type:
234+
All value types have a public parameterless instance constructor called the ***default constructor***. For struct types that do not explicitly declare a parameterless instance constructor, the default constructor is synthesized by the compiler. The default constructor returns a zero-initialized instance known as the ***default value*** for the value type:
235235

236236
- For all *simple_type*s, the default value is the value produced by a bit pattern of all zeros:
237237
- For `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `nint`, `nuint`, `long`, and `ulong`, the default value is `0`.
@@ -246,7 +246,7 @@ All value types implicitly declare a public parameterless instance constructor c
246246

247247
Like any other instance constructor, the default constructor of a value type is invoked using the `new` operator.
248248

249-
> *Note*: For efficiency reasons, this requirement is not intended to actually have the implementation generate a constructor call. For value types, the default value expression ([§12.8.21](expressions.md#12821-default-value-expressions)) produces the same result as using the default constructor. *end note*
249+
> *Note*: For efficiency reasons, this requirement is not intended to actually have the implementation generate a constructor call. For value types that do not have an explicitly declared parameterless instance constructor, the default value expression ([§12.8.21](expressions.md#12821-default-value-expressions)) produces the same result as using the default constructor. For struct types that declare an explicit parameterless instance constructor, `default` produces the zero-initialized default value, while `new S()` invokes the declared constructor, and the results may differ. *end note*
250250
<!-- markdownlint-disable MD028 -->
251251
252252
<!-- markdownlint-enable MD028 -->
@@ -267,7 +267,7 @@ Like any other instance constructor, the default constructor of a value type is
267267
>
268268
> *end example*
269269
270-
Because every value type implicitly has a public parameterless instance constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. A struct type is however permitted to declare parameterized instance constructors ([§16.5.9](structs.md#1659-constructors)).
270+
A struct type is permitted to declare instance constructors, including a parameterless instance constructor. An explicitly declared parameterless instance constructor shall have public accessibility ([§16.5.9](structs.md#1659-constructors)).
271271
272272
### 8.3.4 Struct types
273273

0 commit comments

Comments
 (0)