-
Notifications
You must be signed in to change notification settings - Fork 790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ToString() throws an exception for ValueOption<_>when value is ValueNone #7693
Comments
Out of curiosity, does |
Yes, string x and sprintf “%O” x give the same error, as a result of them calling the underlying .ToString() method I assume.
…Sent from my iPhone
On 4 Oct 2019, at 20:08, Abel Braaksma <notifications@github.com<mailto:notifications@github.com>> wrote:
Out of curiosity, does string x give the same error? And what about `sprintf "%o" x"? Imo this is probably a bug (or a regression if it worked before).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#7693?email_source=notifications&email_token=AKGTZ376W6U7YW7WHJWBU4TQM6IDDA5CNFSM4I5HBAT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAMTW7Y#issuecomment-538524543>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AKGTZ34D7AHXB4VAF657GC3QM6IDDANCNFSM4I5HBATQ>.
|
If I understand the related linked issues correctly, The question remains what we should print for |
@abelbraaksma: you've misunderstood. |
@rca22, I should have been clearer that I tried to summarize the discussions in the other thread. I don't agree with "<null>", as a value can never be null. I think full parity is not possible, because currently, option with |
@abelbraaksma No: option with |
The current PR gives This is because Option compiles the |
In that case, I agree it is a very good reason to display ValueNone.
Richard
…Sent from my iPhone
On 9 Oct 2019, at 20:01, Phillip Carter <notifications@github.com<mailto:notifications@github.com>> wrote:
The current PR gives "ValueNone" as per here: https://github.com/dotnet/fsharp/pull/7712/files#diff-1ff43febb9aa3f01fb97440321fb1507R226-R228
This is because Option compiles the None case down into a null, but ValueOption does not because it is a struct.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#7693?email_source=notifications&email_token=AKGTZ36SX3BWXXL2QW5MGE3QNYMA7A5CNFSM4I5HBAT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAYY2UI#issuecomment-540118353>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AKGTZ3YVCNYXK43EKYRWPJDQNYMA7ANCNFSM4I5HBATQ>.
|
@rca22, not on my current version of VS 2019, 16.3.1, which I think is very recent. I don't know how you tested it, but I cannot repro the non-error scenario: > None.ToString();;
System.NullReferenceException: Object reference not set to an instance of an object.
at <StartupCode$FSI_0017>.$FSI_0017.main@()
Stopped due to error
> let x = None in x.ToString();;
System.NullReferenceException: Object reference not set to an instance of an object.
at <StartupCode$FSI_0019>.$FSI_0019.main@()
Stopped due to error
> sprintf "%O" None;;
val it : string = "<null>"
> sprintf "%A" None;;
val it : string = "<null>"
> ValueNone.ToString();;
System.InvalidOperationException: ValueOption.Value
at Microsoft.FSharp.Core.FSharpValueOption`1.get_Value() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2934
at Microsoft.FSharp.Core.FSharpValueOption`1.ToString() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2951
at <StartupCode$FSI_0020>.$FSI_0020.main@()
Stopped due to error
> let x = ValueNone in x.ToString();;
System.InvalidOperationException: ValueOption.Value
at Microsoft.FSharp.Core.FSharpValueOption`1.get_Value() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2934
at Microsoft.FSharp.Core.FSharpValueOption`1.ToString() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2951
at <StartupCode$FSI_0021>.$FSI_0021.main@()
Stopped due to error
> sprintf "%O" ValueNone;;
System.InvalidOperationException: ValueOption.Value
at Microsoft.FSharp.Core.FSharpValueOption`1.get_Value() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2934
at Microsoft.FSharp.Core.FSharpValueOption`1.ToString() in E:\A\_work\159\s\src\fsharp\FSharp.Core\prim-types.fs:line 2951
at Microsoft.FSharp.Core.PrintfImpl.FinalFast1@259.Invoke(FSharpFunc`2 env, A a) in E:\A\_work\159\s\src\fsharp\FSharp.Core\printf.fs:line 262
at <StartupCode$FSI_0022>.$FSI_0022.main@()
Stopped due to error
> sprintf "%A" ValueNone;;
val it : string = "ValueNone" In other words, currently, only |
@abelbraaksma apologies for the belated response. I tried the exact same code for testing
Whereas for IMO the NullReferenceException thrown by Option is a bug in the compiler optimisation to use null, and shouldn't happen either! After all, if I defined something logically identical, |
While I agree it is sensible to expect no error, ToString is an overridden member, and requires an instance, hence it doesn't work. Though in F# we shouldn't have nulls and I think it is fair to expect this to work without exception. |
Good point... |
@KevinRansom, can we summarize what the resolution was, considering this discussion went in several directions? I.e., ideally it never throws now on either |
Just happened to hit this issue again, so let me answer my own question, in case others are wondering what was implemented:
|
The following code throws
The behaviour should be comparable to the option type where
Does not throw and sets
y = "<null>"
This is a real pain when logging value in text files, as you expect sprintf "%O" to work for all F# types. Perhaps you could simply override .ToString() for ValueOption?
This is FSharp.Core 4.6.2, .Net Framework 4.8
The text was updated successfully, but these errors were encountered: