Skip to content

Commit 96fc7b0

Browse files
cartermpbaronfel
authored andcommitted
Voption tostring and other stuff (#7712)
* Fix some issues in ValueNone implementation * More debug display * Some for options in debug display * Apply suggestions from code review Co-Authored-By: Eugene Auduchinok <eugene.auduchinok@gmail.com> * Probably better stringing * Add them baselines * Add sprintfn tests * Update OptionModule.fs
1 parent cb65c31 commit 96fc7b0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/fsharp/FSharp.Core/prim-types.fs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,7 +2883,7 @@ namespace Microsoft.FSharp.Core
28832883
//-------------------------------------------------------------------------
28842884

28852885
[<DefaultAugmentation(false)>]
2886-
[<DebuggerDisplay("Some({Value})")>]
2886+
[<DebuggerDisplay("{DebugDisplay,nq}")>]
28872887
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
28882888
[<CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId="Option")>]
28892889
[<StructuralEquality; StructuralComparison>]
@@ -2907,6 +2907,11 @@ namespace Microsoft.FSharp.Core
29072907
static member Some (value) : 'T option = Some(value)
29082908

29092909
static member op_Implicit (value) : 'T option = Some(value)
2910+
2911+
member private x.DebugDisplay =
2912+
match x with
2913+
| None -> "None"
2914+
| Some _ -> String.Format("Some({0})", anyToStringShowingNull x.Value)
29102915

29112916
override x.ToString() =
29122917
// x is non-null, hence Some
@@ -2924,7 +2929,7 @@ namespace Microsoft.FSharp.Core
29242929
[<StructuralEquality; StructuralComparison>]
29252930
[<Struct>]
29262931
[<CompiledName("FSharpValueOption`1")>]
2927-
[<DebuggerDisplay("ValueSome({Value})")>]
2932+
[<DebuggerDisplay("{DebugDisplay,nq}")>]
29282933
type ValueOption<'T> =
29292934
| ValueNone : 'T voption
29302935
| ValueSome : 'T -> 'T voption
@@ -2942,11 +2947,17 @@ namespace Microsoft.FSharp.Core
29422947
[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
29432948
member x.IsSome = match x with ValueSome _ -> true | _ -> false
29442949

2945-
static member op_Implicit (value) : 'T option = Some(value)
2946-
2947-
override x.ToString() =
2948-
// x is non-null, hence ValueSome
2949-
"ValueSome("^anyToStringShowingNull x.Value^")"
2950+
static member op_Implicit (value) : 'T voption = ValueSome(value)
2951+
2952+
member private x.DebugDisplay =
2953+
match x with
2954+
| ValueNone -> "ValueNone"
2955+
| ValueSome _ -> String.Format("ValueSome({0})", anyToStringShowingNull x.Value)
2956+
2957+
override x.ToString() =
2958+
match x with
2959+
| ValueNone -> "ValueNone"
2960+
| ValueSome _ -> anyToStringShowingNull x.Value
29502961

29512962
and 'T voption = ValueOption<'T>
29522963

src/fsharp/FSharp.Core/prim-types.fsi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,11 @@ namespace Microsoft.FSharp.Core
18601860

18611861
/// <summary>Return 'true' if the value option is a 'ValueNone' value.</summary>
18621862
member IsNone : bool
1863+
1864+
/// <summary>Implicitly converts a value into an optional that is a 'ValueSome' value.</summary>
1865+
/// <param name="value">The input value</param>
1866+
/// <returns>A voption representing the value.</returns>
1867+
static member op_Implicit: value: 'T -> 'T voption
18631868

18641869
/// <summary>The type of optional values, represented as structs.</summary>
18651870
///

0 commit comments

Comments
 (0)