diff --git a/src/TrueMyth/Maybe.cs b/src/TrueMyth/Maybe.cs
index 0fdd39c..0bd09ed 100644
--- a/src/TrueMyth/Maybe.cs
+++ b/src/TrueMyth/Maybe.cs
@@ -4,6 +4,8 @@
namespace TrueMyth
{
+ using Unsafe;
+
///
/// A static class that provides factory and extension methods for .
///
@@ -361,12 +363,6 @@ public void Match(Action just, Action nothing)
///
public Result ToResult(Func errFn) => this._isJust ? Result.Ok(this._value) : Result.Err(errFn());
- ///
- /// Get the TValue value out of the Maybe<TValue>. Returns the content of a Just, but throws if the Maybe is Nothing.
- /// Prefer to use or .
- ///
- public TValue UnsafelyUnwrap() => this._isJust ? this._value : throw new InvalidOperationException($"Invalid attempt to unwrap {GetType().Name}.Nothing as {typeof(TValue).Name}");
-
///
/// Safely get the TValue value out of the Maybe<TValue>. Returns the content of Just or defaultValue if this is Nothing.
/// This is the recommended way to get a value out of a Maybe most of the time.
@@ -457,7 +453,7 @@ public int CompareTo(Maybe otherMaybe)
{
if (typeof(IComparable).IsAssignableFrom(typeof(TValue)))
{
- var justThis = UnsafelyUnwrap() as IComparable;
+ var justThis = this.UnsafelyUnwrap() as IComparable;
var justThat = otherMaybe.UnsafelyUnwrap();
return justThis.CompareTo(justThat);
}
@@ -485,8 +481,8 @@ public int CompareTo(object obj)
#region Public Static Methods & Operators
///
- /// Equivalent of . Follows usual C♯ semantics of throwing
- /// an exception at runtime if the conversion is invalid.
+ /// Equivalent of . Follows usual C♯ semantics
+ /// of throwing an exception at runtime if the conversion is invalid.
///
public static explicit operator TValue(Maybe maybe) => maybe.UnsafelyUnwrap();
diff --git a/src/TrueMyth/Result.cs b/src/TrueMyth/Result.cs
index a76da5e..d8bc456 100644
--- a/src/TrueMyth/Result.cs
+++ b/src/TrueMyth/Result.cs
@@ -3,6 +3,8 @@
namespace TrueMyth
{
+ using Unsafe;
+
///
/// A static class that provides factory and extension methods for .
///
@@ -452,18 +454,6 @@ public void Match(Action ok, Action err)
///
public Maybe ToMaybe() => this._isOk ? Maybe.Of(this._value) : Maybe.Nothing;
- ///
- /// Get the value out of the Result. Returns the content of an Ok but throws if the result is Err.
- /// Prefer to use or
- ///
- public TValue UnsafelyUnwrap() => _isOk ? _value : throw new InvalidOperationException("Invalid request to unwrap value.");
-
- ///
- /// Get the error out of the Result. Returns the content of an Err, but throws if the result is Ok.
- /// Prefer to use .
- ///
- public TError UnsafelyUnwrapErr() => !_isOk ? _error : throw new InvalidOperationException("Invalid request to unwrap error.");
-
///
/// Safely get the value out of the Ok variant of a Result. This is the recommended way to get a value
/// out of a Result most of the time.
@@ -472,13 +462,13 @@ public void Match(Action ok, Action err)
///
///
/// var anOk = Result<int,string>.Ok(1);
- /// Console.WriteLine(anOk.Unwrap(0)); // 1
+ /// Console.WriteLine(anOk.UnwrapOr(0)); // 1
///
/// var anErr = Result<int,string>.Err("error");
- /// Console.WriteLine(anErr.Unwrap(0)); // 0
+ /// Console.WriteLine(anErr.UnwrapOr(0)); // 0
///
///
- public TValue Unwrap(TValue defaultValue) => this._isOk ? _value : defaultValue;
+ public TValue UnwrapOr(TValue defaultValue) => this._isOk ? _value : defaultValue;
///
/// Safely get the value out of a Result<TValue,TError> by returning the wrapped value if it is Ok
@@ -494,13 +484,13 @@ public void Match(Action ok, Action err)
/// var handleError = (string err) => err.Length + someOtherValue;
///
/// var anOk = Result<int,string>.Ok(42);
- /// Console.WriteLine(anOk.Unwrap(handleError)); // 42
+ /// Console.WriteLine(anOk.UnwrapOrElse(handleError)); // 42
///
/// var anErr = Result<int,string>.Err("error");
- /// Console.WriteLine(anErr.Unwrap(handleError)); // error
+ /// Console.WriteLine(anErr.UnwrapOrElse(handleError)); // error
///
///
- public TValue Unwrap(Func elseFn) => this._isOk ? _value : elseFn(this._error);
+ public TValue UnwrapOrElse(Func elseFn) => this._isOk ? _value : elseFn(this._error);
#endregion
@@ -579,7 +569,7 @@ public int CompareTo(Result otherResult)
}
else if(typeof(IComparable).IsAssignableFrom(typeof(TError)))
{
- var thisErr = UnsafelyUnwrapErr() as IComparable;
+ var thisErr = this.UnsafelyUnwrapErr() as IComparable;
var thatErr = otherResult.UnsafelyUnwrapErr();
return thisErr.CompareTo(thatErr);
}
@@ -596,7 +586,7 @@ public int CompareTo(Result otherResult)
}
else if (typeof(IComparable).IsAssignableFrom(typeof(TValue)))
{
- var thisValue = UnsafelyUnwrap() as IComparable;
+ var thisValue = this.UnsafelyUnwrap() as IComparable;
var thatValue = otherResult.UnsafelyUnwrap();
return thisValue.CompareTo(thatValue);
}
diff --git a/src/TrueMyth/Unsafe/UnsafeExtensions.cs b/src/TrueMyth/Unsafe/UnsafeExtensions.cs
new file mode 100644
index 0000000..4683a8f
--- /dev/null
+++ b/src/TrueMyth/Unsafe/UnsafeExtensions.cs
@@ -0,0 +1,30 @@
+using System;
+using TrueMyth;
+
+namespace TrueMyth.Unsafe
+{
+ ///
+ /// Extensions to the and implementations; the use of these
+ /// methods is discouraged by organizing them this way.
+ ///
+ public static class UnsafeExtensions
+ {
+ ///
+ /// Get the TValue value out of the Maybe<TValue>. Returns the content of a Just, but throws if the Maybe is Nothing.
+ /// Prefer to use or .
+ ///
+ public static T UnsafelyUnwrap(this Maybe maybe) => maybe.UnwrapOrElse(() => throw new InvalidOperationException($"Invalid attempt to unwrap {maybe.GetType().Name}.Nothing as {typeof(T).Name}"));
+
+ ///
+ /// Get the value out of the Result. Returns the content of an Ok but throws if the result is Err.
+ /// Prefer to use or
+ ///
+ public static T UnsafelyUnwrap(this Result result) => result.UnwrapOrElse((err) => throw new InvalidOperationException("Invalid request to unwrap value."));
+
+ ///
+ /// Get the error out of the Result. Returns the content of an Err, but throws if the result is Ok.
+ /// Prefer to use .
+ ///
+ public static E UnsafelyUnwrapErr(this Result result) => result.Match(t => throw new InvalidOperationException("Invalid request to unwrap error."), e => e);
+ }
+}
\ No newline at end of file
diff --git a/test/TrueMyth.Test/MaybeTests.cs b/test/TrueMyth.Test/MaybeTests.cs
index 7eb4355..f19d4aa 100644
--- a/test/TrueMyth.Test/MaybeTests.cs
+++ b/test/TrueMyth.Test/MaybeTests.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using Xunit;
using TrueMyth;
+using TrueMyth.Unsafe;
namespace TrueMyth.Test
{
diff --git a/test/TrueMyth.Test/ResultTests.cs b/test/TrueMyth.Test/ResultTests.cs
index 260ad73..8a55856 100644
--- a/test/TrueMyth.Test/ResultTests.cs
+++ b/test/TrueMyth.Test/ResultTests.cs
@@ -1,6 +1,7 @@
using System;
using Xunit;
using TrueMyth;
+using TrueMyth.Unsafe;
namespace TrueMyth.Test
{
@@ -357,7 +358,7 @@ public void UnwrapOr_Ok_Ok()
var r1 = SimpleResult.Ok(7);
// act
- var r = r1.Unwrap(0);
+ var r = r1.UnwrapOr(0);
// assert
Assert.Equal(7, r);
@@ -370,7 +371,7 @@ public void UnwrapOr_Err_Ok()
var r1 = SimpleResult.Err("error");
// act
- var r = r1.Unwrap(0);
+ var r = r1.UnwrapOr(0);
// assert
Assert.Equal(0, r);
@@ -384,7 +385,7 @@ public void UnwrapOrElse_Ok_Ok()
var r1 = SimpleResult.Ok(7);
// act
- var r = r1.Unwrap(err => 0);
+ var r = r1.UnwrapOrElse(err => 0);
// assert
Assert.Equal(7, r);
@@ -397,7 +398,7 @@ public void UnwrapOrElse_Err_Ok()
var r1 = SimpleResult.Err("error");
// act
- var r = r1.Unwrap(err => 0);
+ var r = r1.UnwrapOrElse(err => 0);
// assert
Assert.Equal(0, r);