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:
Please tick all that apply:
Print "None" when value is
NoneI propose we normalize how
optionis treated when turning its value into a string, namely,string Noneshould 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 isNone. Conversely, when debugging,Nonewill appear asnull, 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 genericstringfunction in such a way that it works differently foroption<_>.Pros and Cons
The advantages of making this adjustment to F# are:
nullwhen they inspect a variable that isNone. This would help the general debugging experience.None?" Until it dawns on you after some headbanging.ValueOptions.The disadvantages of making this adjustment to F# are :
string, and if there are libraries out there that rely onstring None = "", these would failoptionis converted to anobj(boxed), the old behavior remains, as it is impossible to detect the difference between anullobject reference and anullvalue that once wasNone. 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:
ValueNone: ToString() throws an exception for ValueOption<_>when value is ValueNone dotnet/fsharp#7693optionsandValueOption: Voption tostring and other stuff dotnet/fsharp#7712 (note that this PR doesn't work anymore forNone)Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply: