Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/fsharp/FSharp.Core/prim-types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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>]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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>

Expand Down
5 changes: 5 additions & 0 deletions src/fsharp/FSharp.Core/prim-types.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -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>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Assert.AreEqual(ValueOption.map fn (ValueSome 5), ValueOption.bind (fn >> ValueSome) (ValueSome 5))
1 change: 1 addition & 0 deletions tests/FSharp.Core.UnitTests/SurfaceArea.coreclr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/FSharp.Core.UnitTests/SurfaceArea.net40.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down