提交 99849378 编写于 作者: P Phillip Carter 提交者: Kevin Ransom (msft)

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: NEugene Auduchinok <eugene.auduchinok@gmail.com>

* Probably better stringing

* Add them baselines

* Add sprintfn tests

* Update OptionModule.fs
上级 cdc391e4
......@@ -2883,7 +2883,7 @@ namespace Microsoft.FSharp.Core
//-------------------------------------------------------------------------
[<DefaultAugmentation(false)>]
[<DebuggerDisplay("Some({Value})")>]
[<DebuggerDisplay("{DebugDisplay,nq}")>]
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
[<CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId="Option")>]
[<StructuralEquality; StructuralComparison>]
......@@ -2907,6 +2907,11 @@ namespace Microsoft.FSharp.Core
static member Some (value) : 'T option = Some(value)
static member op_Implicit (value) : 'T option = Some(value)
member private x.DebugDisplay =
match x with
| None -> "None"
| Some _ -> String.Format("Some({0})", anyToStringShowingNull x.Value)
override x.ToString() =
// x is non-null, hence Some
......@@ -2924,7 +2929,7 @@ namespace Microsoft.FSharp.Core
[<StructuralEquality; StructuralComparison>]
[<Struct>]
[<CompiledName("FSharpValueOption`1")>]
[<DebuggerDisplay("ValueSome({Value})")>]
[<DebuggerDisplay("{DebugDisplay,nq}")>]
type ValueOption<'T> =
| ValueNone : 'T voption
| ValueSome : 'T -> 'T voption
......@@ -2942,11 +2947,17 @@ namespace Microsoft.FSharp.Core
[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
member x.IsSome = match x with ValueSome _ -> true | _ -> false
static member op_Implicit (value) : 'T option = Some(value)
override x.ToString() =
// x is non-null, hence ValueSome
"ValueSome("^anyToStringShowingNull x.Value^")"
static member op_Implicit (value) : 'T voption = ValueSome(value)
member private x.DebugDisplay =
match x with
| ValueNone -> "ValueNone"
| ValueSome _ -> String.Format("ValueSome({0})", anyToStringShowingNull x.Value)
override x.ToString() =
match x with
| ValueNone -> "ValueNone"
| ValueSome _ -> anyToStringShowingNull x.Value
and 'T voption = ValueOption<'T>
......
......@@ -1860,6 +1860,11 @@ namespace Microsoft.FSharp.Core
/// <summary>Return 'true' if the value option is a 'ValueNone' value.</summary>
member IsNone : bool
/// <summary>Implicitly converts a value into an optional that is a 'ValueSome' value.</summary>
/// <param name="value">The input value</param>
/// <returns>A voption representing the value.</returns>
static member op_Implicit: value: 'T -> 'T voption
/// <summary>The type of optional values, represented as structs.</summary>
///
......
......@@ -222,6 +222,16 @@ type ValueOptionTests() =
let assertWasNotCalledThunk () = raise (exn "Thunk should not have been called.")
[<Test>]
member _.``ValueNone gives "ValueNone" when calling ToString`` () =
Assert.AreEqual("ValueNone", ValueNone.ToString())
Assert.AreEqual("ValueNone", string ValueNone)
[<Test>]
member _.``ValueNone with sprintf`` () =
Assert.AreEqual("ValueNone", sprintf "%O" (ValueNone.ToString()))
Assert.AreEqual("ValueNone", sprintf "%A" ValueNone)
[<Test>]
member this.ValueOptionBasics () =
Assert.AreEqual((ValueNone: int voption), (ValueNone: int voption))
......@@ -444,4 +454,4 @@ type ValueOptionTests() =
member this.MapBindEquivalenceProperties () =
let fn x = x + 3
Assert.AreEqual(ValueOption.map fn ValueNone, ValueOption.bind (fn >> ValueSome) ValueNone)
Assert.AreEqual(ValueOption.map fn (ValueSome 5), ValueOption.bind (fn >> ValueSome) (ValueSome 5))
\ No newline at end of file
Assert.AreEqual(ValueOption.map fn (ValueSome 5), ValueOption.bind (fn >> ValueSome) (ValueSome 5))
......@@ -1746,6 +1746,7 @@ Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueO
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] ValueNone
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] get_None()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] get_ValueNone()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] op_Implicit(T)
Microsoft.FSharp.Core.FSharpValueOption`1[T]: System.String ToString()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: T Item
Microsoft.FSharp.Core.FSharpValueOption`1[T]: T Value
......
......@@ -1746,6 +1746,7 @@ Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueO
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] ValueNone
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] get_None()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] get_ValueNone()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: Microsoft.FSharp.Core.FSharpValueOption`1[T] op_Implicit(T)
Microsoft.FSharp.Core.FSharpValueOption`1[T]: System.String ToString()
Microsoft.FSharp.Core.FSharpValueOption`1[T]: T Item
Microsoft.FSharp.Core.FSharpValueOption`1[T]: T Value
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册