Skip to content

ToString() throws an exception for ValueOption<_>when value is ValueNone #7693

@rca22

Description

@rca22

The following code throws

let x : ValueOption<int> = ValueNone
let y = x.ToString() 

System.InvalidOperationException: ValueOption.Value
   at Microsoft.FSharp.Core.FSharpValueOption`1.get_Value()
   at Microsoft.FSharp.Core.FSharpValueOption`1.ToString()
   at Microsoft.FSharp.Core.PrintfImpl.FinalFast1@264.Invoke(FSharpFunc`2 env, A a)
   at <StartupCode$FSI_0013>.$FSI_0013.main@()

The behaviour should be comparable to the option type where

let x : Option<int> = None
let y = x.ToString()

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-LibraryIssues for FSharp.Core not covered elsewhereBug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions