From c9b9959642ad811f77ac0ef06b72d283d1b9b1cb Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 23 Dec 2016 11:04:57 +0100 Subject: [PATCH 1/2] Return string value of Some or blank in Option.ToString() This renders Option.ToString() consistent with Nullable.ToString() and easier to substitute for. --- Optional.Tests/MaybeTests.cs | 18 +++++++++--------- Optional/Option_Maybe.cs | 12 +----------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Optional.Tests/MaybeTests.cs b/Optional.Tests/MaybeTests.cs index 93f30c8..29d12e3 100644 --- a/Optional.Tests/MaybeTests.cs +++ b/Optional.Tests/MaybeTests.cs @@ -150,19 +150,19 @@ public void Maybe_Hashing() [TestMethod] public void Maybe_StringRepresentation() { - Assert.AreEqual(Option.None().ToString(), "None"); - Assert.AreEqual(Option.None().ToString(), "None"); - Assert.AreEqual(Option.None().ToString(), "None"); + Assert.AreEqual(Option.None().ToString(), ""); + Assert.AreEqual(Option.None().ToString(), ""); + Assert.AreEqual(Option.None().ToString(), ""); - Assert.AreEqual(Option.Some(null).ToString(), "Some(null)"); - Assert.AreEqual(Option.Some(null).ToString(), "Some(null)"); + Assert.AreEqual(Option.Some(null).ToString(), ""); + Assert.AreEqual(Option.Some(null).ToString(), ""); - Assert.AreEqual(Option.Some(1).ToString(), "Some(1)"); - Assert.AreEqual(Option.Some(1).ToString(), "Some(1)"); - Assert.AreEqual(Option.Some("1").ToString(), "Some(1)"); + Assert.AreEqual(Option.Some(1).ToString(), "1"); + Assert.AreEqual(Option.Some(1).ToString(), "1"); + Assert.AreEqual(Option.Some("1").ToString(), "1"); var now = DateTime.Now; - Assert.AreEqual(Option.Some(now).ToString(), "Some(" + now.ToString() + ")"); + Assert.AreEqual(Option.Some(now).ToString(), now.ToString()); } [TestMethod] diff --git a/Optional/Option_Maybe.cs b/Optional/Option_Maybe.cs index 8785e1f..71a1581 100644 --- a/Optional/Option_Maybe.cs +++ b/Optional/Option_Maybe.cs @@ -97,17 +97,7 @@ public override int GetHashCode() /// A string that represents the current optional. public override string ToString() { - if (hasValue) - { - if (value == null) - { - return "Some(null)"; - } - - return string.Format("Some({0})", value); - } - - return "None"; + return hasValue && value != null ? value.ToString() : string.Empty; } /// From f4668ce387440134134863463ac69b0eb15ebef7 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 23 Dec 2016 11:27:05 +0100 Subject: [PATCH 2/2] DebuggerDisplay for Option --- Optional/Option_Maybe.cs | 20 ++++++++++++++++++++ Portable.Optional/project.json | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Optional/Option_Maybe.cs b/Optional/Option_Maybe.cs index 71a1581..f1d7417 100644 --- a/Optional/Option_Maybe.cs +++ b/Optional/Option_Maybe.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; @@ -12,6 +13,7 @@ namespace Optional #if !NETSTANDARD [Serializable] #endif + [DebuggerDisplay("{" + nameof(DebugString) + "}")] public struct Option : IEquatable> { private readonly bool hasValue; @@ -91,6 +93,24 @@ public override int GetHashCode() return 0; } + string DebugString + { + get + { + if (hasValue) + { + if (value == null) + { + return "Some(null)"; + } + + return string.Format("Some({0})", value); + } + + return "None"; + } + } + /// /// Returns a string that represents the current optional. /// diff --git a/Portable.Optional/project.json b/Portable.Optional/project.json index 68f1424..905c2ea 100644 --- a/Portable.Optional/project.json +++ b/Portable.Optional/project.json @@ -3,7 +3,8 @@ "dependencies": { "Microsoft.NETCore.Portable.Compatibility": "1.0.1", "Microsoft.NETCore.Platforms": "1.0.1", - "System.Collections": "4.0.11" + "System.Collections": "4.0.11", + "System.Diagnostics.Debug": "4.0.11" }, "frameworks": { "netstandard1.0": {}