Skip to content

Return "None" when doing string None, instead of empty string #891

@abelbraaksma

Description

@abelbraaksma

Print "None" when value is None

I propose we normalize how option is treated when turning its value into a string, namely, string None should return "None". Currently it returns the empty string.

This would bring it more in line with this existing change (dotnet/fsharp#7693) which returns "ValueNone" for string ValueNone. It also brings it in line with FSI, which prints "None" to the output window when it is None. Conversely, when debugging, None will appear as null, even though this PR was done to mitigate this.

The existing way of approaching this problem in F# is:

Roll your own version to get the string value of an option. But this doesn't help with debugging. It's also not very trivial to re-implement a generic string function in such a way that it works differently for option<_>.

Pros and Cons

The advantages of making this adjustment to F# are:

  • Many newcomers to the language are very surprised to see null when they inspect a variable that is None. This would help the general debugging experience.
  • When printing an option, you get "Some ..." in the output, but never "None", this leads to surprises: "why is this not None?" Until it dawns on you after some headbanging.
  • Alignment with other discriminated unions, that always print the case-name and the value. Most notably, "ValueNone" for ValueOptions.

The disadvantages of making this adjustment to F# are :

  • It's a tiny change in behavior for string, and if there are libraries out there that rely on string None = "", these would fail
  • If an option is converted to an obj (boxed), the old behavior remains, as it is impossible to detect the difference between a null object reference and a null value that once was None. But this problem applies in other parts of the .NET ecosystem as well.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): XS (proto-type is ready, link to PR follows)

I remember earlier discussions on this and that there was no simple solution, but perhaps that was about None.ToString(), which raises an NRE. I didn't know that there was a type-safe syntax form, albeit compiler-specific, that makes this work. It is possible that in the past this syntax wasn't available, but now that it is, I suggest we make use of it.

I've also checked to fix None.ToString() and the like, but that's a wider problem and should go in its own suggestion.

Related suggestions:

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:

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