Skip to content
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

Propose relaxed ordering for partial and ref modifiers #8134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 131 additions & 6 deletions proposals/partial-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ https://github.com/dotnet/csharplang/issues/6420

### Grammar

The *property_declaration* grammar [(§14.7.1)](https://github.com/dotnet/csharpstandard/blob/draft-v7/standard/classes.md#1471-general) is updated as follows:
The partial modifier on properties is treated just like any other modifier and can be included in any position in a modifier list.

The property grammar [(§15.7.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1571-general) is updated as follows:

```diff
property_declaration
- : attributes? property_modifier* type member_name property_body
+ : attributes? property_modifier* 'partial'? type member_name property_body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading diffs of diffs can be challenging even in simple cases like this, so consider just reading the old version then the new version to understand what's going on.

;
property_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| 'static'
| 'virtual'
| 'sealed'
| 'override'
| 'abstract'
| 'extern'
+ | 'partial'
;
```

**Remarks**: This is somewhat similar to how *method_header* [(§15.6.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1561-general) and *class_declaration* [(§15.2.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1521-general) are specified. (Note that [Issue #946](https://github.com/dotnet/csharplang/issues/946) proposes to relax the ordering requirement, and would probably apply to all declarations which allow the `partial` modifier. We intend to specify such an ordering relaxation in the near future, and implement it in the same release that this feature is implemented.)
See also [Relaxed ordering requirement](#relaxed-ordering-requirement) for similar changes to the method, class, struct and interface grammars.

### Defining and implementing declarations
When a property declaration includes a *partial* modifier, that property is said to be a *partial property*. Partial properties may only be declared as members of partial types.
Expand Down Expand Up @@ -226,6 +238,119 @@ partial class C
}
```

### Relaxed ordering requirement

- https://github.com/dotnet/csharplang/issues/946
- https://github.com/dotnet/csharplang/blob/main/meetings/2020/LDM-2020-09-09.md#champion-relax-ordering-constraints-around-ref-and-partial-modifiers-on-type-declarations

In C# 12 and prior, `partial` can only be used as the last modifier before a method return type, or as the last modifier before type declaration keywords `class`, `struct`, or `interface`. Similar restrictions are in place for the `ref` modifier on a `struct` declaration.

In C# 13, `partial` will become just like any other modifier on the above-mentioned declarations, and can appear in any order in the modifier list. The same behavior will apply for properties. The `ref` modifier will be similarly relaxed on `struct` declarations.

#### Detailed design

The property grammar is given in [Grammar](#grammar).

The method grammar [(§15.6.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1561-general) is updated as follows:

```diff
method_modifiers
- : method_modifier* 'partial'?
+ : method_modifier*
;

method_modifier
: ref_method_modifier
| 'async'
;

ref_method_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| 'static'
| 'virtual'
| 'sealed'
| 'override'
| 'abstract'
| 'extern'
+ | 'partial'
| unsafe_modifier // unsafe code support
;
```

The classes grammar [(§15.2.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1521-general) is updated as follows:

```diff
class_declaration
- : attributes? class_modifier* 'partial'? 'class' identifier
+ : attributes? class_modifier* 'class' identifier
type_parameter_list? class_base? type_parameter_constraints_clause*
class_body ';'?
;

class_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| 'abstract'
| 'sealed'
| 'static'
+ | 'partial'
| unsafe_modifier // unsafe code support
;
```

The interfaces grammar [(§18.2.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/interfaces.md#1821-general) is updated as follows:

```diff
interface_declaration
- : attributes? interface_modifier* 'partial'? 'interface'
+ : attributes? interface_modifier* 'interface'
identifier variant_type_parameter_list? interface_base?
type_parameter_constraints_clause* interface_body ';'?
;

interface_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
+ | 'partial'
| unsafe_modifier // unsafe code support
;
```

The structs grammar [(§18.2.1)](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/structs.md#1621-general) is updated as follows:

```diff
struct_declaration
- : attributes? struct_modifier* 'ref'? 'partial'? 'struct'
+ : attributes? struct_modifier* 'struct'
identifier type_parameter_list? struct_interfaces?
type_parameter_constraints_clause* struct_body ';'?
;
```

```diff
struct_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| 'readonly'
+ | 'ref'
+ | 'partial'
| unsafe_modifier // unsafe code support
;
```

## Open Issues

### Other member kinds
Expand Down