Skip to content

Improve interop to Nullable optional parameters #774

@dsyme

Description

@dsyme

I propose we improve interop to C#-defined Nullable-typed optional parameters

Consider a C# definition

        public int SomeMethod(int? channels = 0, int? ratio = 1)

The compiled form of this uses Nullable typed values for the parameters with default value nullref.

We do not recognise this in F# and when calling and providing a value we require

    C.SomeMethod(channels = Nullable 3)
    C.SomeMethod(channels = Nullable 3, ratio = Nullable 3)
    C.SomeMethod(ratio = Nullable 3)

Alternatives that achieve the same effect are

  1. to allow auto-conversion from T to Nullable<T> at method calls. Indeed this is probably the best solution.

  2. some even more aggressive auto-conversion rule for T to Nullable<T>

Pros and Cons

The advantages of making this adjustment to F# are interop with modern C# becomes smoother.

The disadvantages of making this adjustment to F# are

  • another implicit conversion rule is added.
  • we have to be careful this is not a breaking change

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions