Skip to content

Commit

Permalink
Update FS-1127-init-only-and-required-properties.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyme authored Jul 12, 2022
1 parent 1f570bb commit fb70051
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions RFCs/FS-1127-init-only-and-required-properties.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# F# RFC FS-XXXX - Init-only setters and required properties support in F# #
# F# RFC FS-1127 - Init-only setters and required properties support in F# #

This RFC covers the detailed proposal.
The design suggestion [C# record interop (including init only properties of .net 5 and required of .net 7)](https://github.com/fsharp/fslang-suggestions/issues/904) has been approved in principle. This RFC covers the detailed proposal for `init` and `requried` properties.

- [x] Implementation: [WIP](https://github.com/dotnet/fsharp/pull/13490)
- [ ] [Discussion](about:blank)
- [ ] [Suggestion](about:blank)

## Summary #

- [Init-only setters](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/init) is a property setter, which can only be called at the time of object initialization. Currently, F# ignores it and allows caller to call an init-only setter after an object has been created.
- F# compiler will restrict the call of an init-only setter to the object initialization only.

- "Required members" feature was recently added to C# and is a way of specifying that a property or field is required to be set during object initialization, forcing the callee to provide initial values for all required members at the creation side.
- Scope of F# support will be limited to consuming classes with required members and enforcing the initialization at the creation side in the compile time.
- In this RFC, F# support will be limited to consuming classes with required members and enforcing the initialization at the creation side in the compile time.

## Motivation ##

F# compiler should support and respect the contracts, which are implied by the CIL and metadata produced by C#.

<sub>C# motivation for adding required members can be found [here](https://github.com/dotnet/csharplang/blob/main/proposals/required-members.md#motivation).</sub>

## Detailed Design ##

### Init-only property setters ###

Expand Down Expand Up @@ -130,11 +137,6 @@ And in the following codegen for the custom constructor with `SetsRequiredMember
}
```

## Motivation ##

F# compiler should support and respect the contracts, which are implied by the CIL and metadata produced by C#.

<sub>C# motivation for adding required members can be found [here](https://github.com/dotnet/csharplang/blob/main/proposals/required-members.md#motivation).</sub>

## Detailed design ##

Expand Down Expand Up @@ -164,7 +166,7 @@ No alternatives

- Is this a breaking change?

Yes. In a way. Currently, init-only properties can be set outside the initializer in F#, it will be a compile-time error after this change is introduced.
Yes. In a way. Currently, init-only properties can be set outside the initializer in F#, it will be a compile-time error after this change is introduced. However this code should never have been allowed.

- What happens when previous versions of the F# compiler encounter this design addition as source code?

Expand All @@ -181,6 +183,6 @@ No alternatives

## Unresolved questions ##

- Shall both of `init-only` setters and `required` members support be under language feature?
- Shall `required` members support be tied to runtime (technically, it doesn't require any runtime features)?
g
* [ ] Shall both of `init-only` setters and `required` members support be under language feature?
* [ ] Shall `required` members support be tied to runtime (technically, it doesn't require any runtime features)?
* [ ] What happens if you explicitly declare the properties on F# classes or record fields? Is that even possible?

0 comments on commit fb70051

Please sign in to comment.