Added MaybeEx<T, Ex> for exception errors and implicit castings

This commit is contained in:
2025-04-19 15:05:15 -04:00
parent 82428641b6
commit 314b706d61
4 changed files with 140 additions and 1 deletions

View File

@@ -46,6 +46,70 @@ public interface IMaybe<T, E> where E : Error
}
}
public readonly struct MaybeEx<T, Ex> : IMaybe<T, ExceptionError<Ex>> where Ex : Exception
{
[MemberNotNullWhen(true, nameof(Error))]
public readonly bool HasError { get; }
public readonly bool HasValue => !HasError;
public readonly ExceptionError<Ex>? Error { get; init; }
public readonly T Value => HasError ? throw Error.GetException() : _value!;
private readonly T? _value;
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with a default(null) value
/// </summary>
public MaybeEx()
{
_value = default;
}
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with a <paramref name="value"/>
/// </summary>
public MaybeEx(T value)
{
_value = value;
}
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with an exception error <paramref name="e"/>
/// </summary>
public MaybeEx(Ex e)
{
Error = e;
HasError = true;
_value = default;
}
public static implicit operator MaybeEx<T, Ex>(T value)
{
return new MaybeEx<T, Ex>(value);
}
public static implicit operator MaybeEx<T, Ex>(Ex e)
{
return new MaybeEx<T, Ex>(e);
}
public static implicit operator Maybe<T, ExceptionError<Ex>>(MaybeEx<T, Ex> value)
{
return value;
}
public static implicit operator Maybe<T>(MaybeEx<T, Ex> value)
{
return value;
}
public static implicit operator T(MaybeEx<T, Ex> value)
{
if (value.HasError)
throw value.Error.GetException();
return value.Value;
}
}
public readonly struct Maybe<T, E> : IMaybe<T, E> where E: Error
{
[MemberNotNullWhen(true, nameof(Error))]
@@ -93,6 +157,10 @@ public readonly struct Maybe<T, E> : IMaybe<T, E> where E: Error
return new Maybe<T, E>(e);
}
public static implicit operator Maybe<T>(Maybe<T, E> value)
{
return value;
}
public static implicit operator T(Maybe<T, E> value)
{

View File

@@ -8,7 +8,7 @@
<PropertyGroup>
<PackageId>MaybeError</PackageId>
<Version>1.0.6</Version>
<Version>1.1.0</Version>
<Authors>Amatsugu</Authors>
<PackageDescription>Errors as values</PackageDescription>
<RepositoryUrl>https://github.com/Amatsugu/MaybeError</RepositoryUrl>

View File

@@ -70,4 +70,5 @@ public static class MaybeExtensions
return defaultValue;
return predicate(maybe.Value);
}
}

View File

@@ -63,6 +63,11 @@ public readonly struct ValueMaybe<T, E> : IValueMaybe<T, E> where T : struct whe
return new ValueMaybe<T, E>(e);
}
public static implicit operator ValueMaybe<T>(ValueMaybe<T, E> value)
{
return value;
}
public static implicit operator T(ValueMaybe<T, E> value)
{
if (value.HasError)
@@ -140,3 +145,68 @@ public readonly struct ValueMaybe<T> : IValueMaybe<T, Error> where T : struct
return value.Value;
}
}
public readonly struct ValueMaybeEx<T, Ex> : IMaybe<T, ExceptionError<Ex>> where T: struct where Ex : Exception
{
[MemberNotNullWhen(true, nameof(Error))]
public readonly bool HasError { get; }
public readonly bool HasValue => !HasError;
public readonly ExceptionError<Ex>? Error { get; init; }
public readonly T Value => HasError ? throw Error.GetException() : _value!;
private readonly T _value;
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with a default(null) value
/// </summary>
public ValueMaybeEx()
{
_value = default;
}
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with a <paramref name="value"/>
/// </summary>
public ValueMaybeEx(T value)
{
_value = value;
}
/// <summary>
/// Creates a new <see cref="Maybe{T, Ex}"/> with an exception error <paramref name="e"/>
/// </summary>
public ValueMaybeEx(Ex e)
{
Error = e;
HasError = true;
_value = default;
}
public static implicit operator ValueMaybeEx<T, Ex>(T value)
{
return new ValueMaybeEx<T, Ex>(value);
}
public static implicit operator ValueMaybeEx<T, Ex>(Ex e)
{
return new ValueMaybeEx<T, Ex>(e);
}
public static implicit operator ValueMaybe<T, ExceptionError<Ex>>(ValueMaybeEx<T, Ex> value)
{
return value;
}
public static implicit operator ValueMaybe<T>(ValueMaybeEx<T, Ex> value)
{
return value;
}
public static implicit operator T(ValueMaybeEx<T, Ex> value)
{
if (value.HasError)
throw value.Error.GetException();
return value.Value;
}
}