From ce71765cb447e00bb870398f83fab7759beb9647 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Thu, 3 Apr 2025 13:48:36 -0400 Subject: [PATCH] added implicit operator --- MaybeError/Errors/ExceptionError.cs | 5 +++++ MaybeError/Maybe.cs | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/MaybeError/Errors/ExceptionError.cs b/MaybeError/Errors/ExceptionError.cs index c6ec7c9..0264e7e 100644 --- a/MaybeError/Errors/ExceptionError.cs +++ b/MaybeError/Errors/ExceptionError.cs @@ -33,4 +33,9 @@ public class ExceptionError : Error where T : Exception { return $"{Message}\n{Exception}"; } + + public static implicit operator ExceptionError(T exception) + { + return new ExceptionError(exception); + } } \ No newline at end of file diff --git a/MaybeError/Maybe.cs b/MaybeError/Maybe.cs index f617ecf..acfd8c8 100644 --- a/MaybeError/Maybe.cs +++ b/MaybeError/Maybe.cs @@ -51,7 +51,7 @@ public readonly struct Maybe : IMaybe where E: Error [MemberNotNullWhen(true, nameof(Error))] public readonly bool HasError { get; } 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!; private readonly T? _value; @@ -82,6 +82,7 @@ public readonly struct Maybe : IMaybe where E: Error _value = default; } + public static implicit operator Maybe(T value) { return new Maybe(value); @@ -92,6 +93,7 @@ public readonly struct Maybe : IMaybe where E: Error return new Maybe(e); } + public static implicit operator T(Maybe value) { if (value.HasError)