fsharp / fsharp/fslang-suggestions
Allow the initialization of explicit fields when using a primary constructor
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
I propose we allow the initialization of explicit fields when using a primary constructor.
Currently in order to initialize explicit fields, a non-primary constructor is required:
```fsharp
// Explicit fields assigned in constructor
type MyClass =
val mutable a: int
val mutable b: int
new(a0, b0) = { a = a0; b = b0; }
// Explicit fields assigned when using primary constructor
type MyClass2() as this =
[]
val mutable a: int
[]
val mutable b: int
do
this.a <- 5
this.b <- 6
// Explicit fields assigned when using let bindings
type MyClass3 private (c0: int) =
[]
val mutable a: int
[]
val mutable b: int
let c = c0
new(a0, b0, c0) as this =
MyClass3(c0)
then
this.a <- a0
this.b <- b0
[]
type Struct1 =
val mutable a: int
new (a0) = { a = a0 }
```
This proposal is to update the language to allow explicit fields to be initialized when using a primary constructor so that the following is possible:
```fsharp
type MyClass(a0: int, b0: int) =
val mutable a = a0
val mutable b = b0
type MyClass2() =
val mutable a = 5
val mutable b = 6
type MyClass3(a0: int, b0: int, c0: int) =
val mutable a = a0
val mutable b = b0
let c = c0
// Not possible today: A type that has a non-mutable explicit field and a let binding.
type MyClass4(a0: int, c0: int) =
val a = a0
let c = c0
// Same as today. Struct cannot have primary non-parameterless constructor so a non-primary constructor must be used.
[]
type Struct1 =
val mutable a: int
new (a0) = { a = a0 }
// Not possible today: Parameterless struct constructor.
[]
type Struct2 =
val mutable a: int
new () = { a = 5 }
// Alternatively using new inline initialization.
[]
type Struct2() =
val mutable a = 5
```
[Related C# feature](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/parameterless-struct-constructors) for parameterless stuct constructors:
## Pros and Cons
**The advantages of making this adjustment to F# are** ...
- Able to have non-mutable explicit fields and let bindings in the same type
- Able to initialize explicit fields the same way as let bindings
- Less verbose way of initializing explicit fields
**The disadvantages of making this adjustment to F# are** ...
- More syntax
## Extra information
**Estimated cost (XS, S, M, L, XL, XXL):**
**Related suggestions:** (put links to related suggestions here)
## Affidavit (please submit!)
Please tick these items by placing a cross in the box:
- [x] This is not a question (e.g. like one you might ask on [StackOverflow](http://stackoverflow.com)) and I have searched StackOverflow for discussions of this issue
- [x] This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to [the compiler and tooling repository](https://github.com/dotnet/fsharp)
- [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it
- [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
Please tick all that apply:
- [x] This is not a breaking change to the F# language design
- [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reviewing the proposed F# primary-constructor and explicit-field examples, then read the linked C# parameterless-struct proposal for related context. The issue names no implementation files, tests, or compiler entry points; done would require an agreed language design plus corresponding implementation and validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 20/100