diff --git a/MaybeError/Maybe.cs b/MaybeError/Maybe.cs index acfd8c8..b3d9064 100644 --- a/MaybeError/Maybe.cs +++ b/MaybeError/Maybe.cs @@ -46,6 +46,70 @@ public interface IMaybe where E : Error } } +public readonly struct MaybeEx : IMaybe> where Ex : Exception +{ + [MemberNotNullWhen(true, nameof(Error))] + public readonly bool HasError { get; } + public readonly bool HasValue => !HasError; + public readonly ExceptionError? Error { get; init; } + public readonly T Value => HasError ? throw Error.GetException() : _value!; + private readonly T? _value; + + /// + /// Creates a new with a default(null) value + /// + public MaybeEx() + { + _value = default; + } + + /// + /// Creates a new with a + /// + public MaybeEx(T value) + { + _value = value; + } + + /// + /// Creates a new with an exception error + /// + public MaybeEx(Ex e) + { + Error = e; + HasError = true; + _value = default; + } + + + public static implicit operator MaybeEx(T value) + { + return new MaybeEx(value); + } + + public static implicit operator MaybeEx(Ex e) + { + return new MaybeEx(e); + } + + public static implicit operator Maybe>(MaybeEx value) + { + return value; + } + + public static implicit operator Maybe(MaybeEx value) + { + return value; + } + + public static implicit operator T(MaybeEx value) + { + if (value.HasError) + throw value.Error.GetException(); + return value.Value; + } +} + public readonly struct Maybe : IMaybe where E: Error { [MemberNotNullWhen(true, nameof(Error))] @@ -93,6 +157,10 @@ public readonly struct Maybe : IMaybe where E: Error return new Maybe(e); } + public static implicit operator Maybe(Maybe value) + { + return value; + } public static implicit operator T(Maybe value) { diff --git a/MaybeError/MaybeError.csproj b/MaybeError/MaybeError.csproj index 102b304..b1d9954 100644 --- a/MaybeError/MaybeError.csproj +++ b/MaybeError/MaybeError.csproj @@ -8,7 +8,7 @@ MaybeError - 1.0.6 + 1.1.0 Amatsugu Errors as values https://github.com/Amatsugu/MaybeError diff --git a/MaybeError/MaybeExtensions.cs b/MaybeError/MaybeExtensions.cs index 2cc8b73..1a0a833 100644 --- a/MaybeError/MaybeExtensions.cs +++ b/MaybeError/MaybeExtensions.cs @@ -70,4 +70,5 @@ public static class MaybeExtensions return defaultValue; return predicate(maybe.Value); } + } diff --git a/MaybeError/ValueMaybe.cs b/MaybeError/ValueMaybe.cs index 9c91044..b3e998e 100644 --- a/MaybeError/ValueMaybe.cs +++ b/MaybeError/ValueMaybe.cs @@ -63,6 +63,11 @@ public readonly struct ValueMaybe : IValueMaybe where T : struct whe return new ValueMaybe(e); } + public static implicit operator ValueMaybe(ValueMaybe value) + { + return value; + } + public static implicit operator T(ValueMaybe value) { if (value.HasError) @@ -139,4 +144,69 @@ public readonly struct ValueMaybe : IValueMaybe where T : struct throw value.Error.GetException(); return value.Value; } +} + + +public readonly struct ValueMaybeEx : IMaybe> where T: struct where Ex : Exception +{ + [MemberNotNullWhen(true, nameof(Error))] + public readonly bool HasError { get; } + public readonly bool HasValue => !HasError; + public readonly ExceptionError? Error { get; init; } + public readonly T Value => HasError ? throw Error.GetException() : _value!; + private readonly T _value; + + /// + /// Creates a new with a default(null) value + /// + public ValueMaybeEx() + { + _value = default; + } + + /// + /// Creates a new with a + /// + public ValueMaybeEx(T value) + { + _value = value; + } + + /// + /// Creates a new with an exception error + /// + public ValueMaybeEx(Ex e) + { + Error = e; + HasError = true; + _value = default; + } + + + public static implicit operator ValueMaybeEx(T value) + { + return new ValueMaybeEx(value); + } + + public static implicit operator ValueMaybeEx(Ex e) + { + return new ValueMaybeEx(e); + } + + public static implicit operator ValueMaybe>(ValueMaybeEx value) + { + return value; + } + + public static implicit operator ValueMaybe(ValueMaybeEx value) + { + return value; + } + + public static implicit operator T(ValueMaybeEx value) + { + if (value.HasError) + throw value.Error.GetException(); + return value.Value; + } } \ No newline at end of file