fsharp / fsharp/fslang-suggestions

Allow parameterless constructors in structs

Open
#362 4 comments 3 reactions 0 assignees View on GitHub
approved-in-principle area: structs-byrefs-and-span
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Allow parameterless constructors in structs [6673905]

**Submitted by Daniel Robinson on 11/6/2014 12:00:00 AM**
**4 votes on UserVoice prior to migration**

This will simplify the somewhat arcane rules of struct initialization and provide greater parity with class definition syntax. This StackOverflow question (http://stackoverflow.com/questions/12600574/argument-validation-in-f-struct-constructor/12603786#12603786) illustrates the counterintuitiveness of validating constructor args.
```fsharp
[]
type S1 =
val M : int
new(m) = { M = m } then if m < 0 then invalidArg "m" "negative"

// would become:

[]
type S2(m) =
do if m < 0 then invalidArg "m" "negative"
member val M = m
```
A similar change is planned for C# 6.0 (https://roslyn.codeplex.com/discussions/562559).
This would open the door to let/do bindings and auto-properties in structs. As noted in the C# discussion, it requires more precise usage of Unchecked.defaultof vs. new T().
## Response

*\* by fslang-admin on 2/10/2016 12:00:00 AM **

Thanks for the suggestion. See comment below about this feature being pulled from C# 6.0.
Don Syme
F# Language Evolution

**[Original UserVoice Submission](https://fslang.uservoice.com/forums/245727-f-language/suggestions/6673905)**
[Archived Uservoice Comments](../tree/master/archive/suggestion-6673905-allow-parameterless-constructors-in-structs.md#comments)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.