I suggest we ...

Allow parameterless constructors in structs

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.

[<Struct>]
type S1 =
val M : int
new(m) =
{ M = m }
then if m < 0 then invalidArg "m" "negative"

would become:

[<Struct>]
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<T> vs. new T().

4 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Daniel RobinsonDaniel Robinson shared this idea  ·   ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Don SymeDon Syme commented  · 

        C# eventually pulled this feature out of C# 6.0. I think we should not do it in F# for that reason alone.

      F# Language

      Feedback and Knowledge Base