diff --git a/src/fsharp/FSharp.Core/prim-types.fs b/src/fsharp/FSharp.Core/prim-types.fs index 542852e5da9..664e1d4c02a 100644 --- a/src/fsharp/FSharp.Core/prim-types.fs +++ b/src/fsharp/FSharp.Core/prim-types.fs @@ -2883,7 +2883,7 @@ namespace Microsoft.FSharp.Core //------------------------------------------------------------------------- [] - [] + [] [] [] [] @@ -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 [] [] [] - [] + [] type ValueOption<'T> = | ValueNone : 'T voption | ValueSome : 'T -> 'T voption @@ -2942,11 +2947,17 @@ namespace Microsoft.FSharp.Core [] 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> diff --git a/src/fsharp/FSharp.Core/prim-types.fsi b/src/fsharp/FSharp.Core/prim-types.fsi index 7ba3b3ae4d4..cd28701e67e 100644 --- a/src/fsharp/FSharp.Core/prim-types.fsi +++ b/src/fsharp/FSharp.Core/prim-types.fsi @@ -1860,6 +1860,11 @@ namespace Microsoft.FSharp.Core /// Return 'true' if the value option is a 'ValueNone' value. member IsNone : bool + + /// Implicitly converts a value into an optional that is a 'ValueSome' value. + /// The input value + /// A voption representing the value. + static member op_Implicit: value: 'T -> 'T voption /// The type of optional values, represented as structs. /// diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Core/OptionModule.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Core/OptionModule.fs index 3fb4395cd69..eaebc3df744 100644 --- a/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Core/OptionModule.fs +++ b/tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Core/OptionModule.fs @@ -222,6 +222,16 @@ type ValueOptionTests() = let assertWasNotCalledThunk () = raise (exn "Thunk should not have been called.") + [] + member _.``ValueNone gives "ValueNone" when calling ToString`` () = + Assert.AreEqual("ValueNone", ValueNone.ToString()) + Assert.AreEqual("ValueNone", string ValueNone) + + [] + member _.``ValueNone with sprintf`` () = + Assert.AreEqual("ValueNone", sprintf "%O" (ValueNone.ToString())) + Assert.AreEqual("ValueNone", sprintf "%A" ValueNone) + [] 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)) diff --git a/tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs b/tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs index 561871089f0..7e3d81efd29 100644 --- a/tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs +++ b/tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs @@ -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 diff --git a/tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs b/tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs index 7a6f7f9d334..8499ecc0eba 100644 --- a/tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs +++ b/tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs @@ -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