added implicit operator
This commit is contained in:
@@ -33,4 +33,9 @@ public class ExceptionError<T> : Error where T : Exception
|
|||||||
{
|
{
|
||||||
return $"{Message}\n{Exception}";
|
return $"{Message}\n{Exception}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static implicit operator ExceptionError<T>(T exception)
|
||||||
|
{
|
||||||
|
return new ExceptionError<T>(exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ public readonly struct Maybe<T, E> : IMaybe<T, E> where E: Error
|
|||||||
[MemberNotNullWhen(true, nameof(Error))]
|
[MemberNotNullWhen(true, nameof(Error))]
|
||||||
public readonly bool HasError { get; }
|
public readonly bool HasError { get; }
|
||||||
public readonly bool HasValue => !HasError;
|
public readonly bool HasValue => !HasError;
|
||||||
public readonly E? Error { get; }
|
public readonly E? Error { get; init; }
|
||||||
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
public readonly T Value => HasError ? throw Error.GetException() : _value!;
|
||||||
|
|
||||||
private readonly T? _value;
|
private readonly T? _value;
|
||||||
@@ -82,6 +82,7 @@ public readonly struct Maybe<T, E> : IMaybe<T, E> where E: Error
|
|||||||
_value = default;
|
_value = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static implicit operator Maybe<T, E>(T value)
|
public static implicit operator Maybe<T, E>(T value)
|
||||||
{
|
{
|
||||||
return new Maybe<T, E>(value);
|
return new Maybe<T, E>(value);
|
||||||
@@ -92,6 +93,7 @@ public readonly struct Maybe<T, E> : IMaybe<T, E> where E: Error
|
|||||||
return new Maybe<T, E>(e);
|
return new Maybe<T, E>(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static implicit operator T(Maybe<T, E> value)
|
public static implicit operator T(Maybe<T, E> value)
|
||||||
{
|
{
|
||||||
if (value.HasError)
|
if (value.HasError)
|
||||||
|
|||||||
Reference in New Issue
Block a user