From 1d50dc2224483b05c6c6a4996b5d12bf9007de5f Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Wed, 25 Feb 2026 20:16:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(result):=20=E4=BF=AE=E5=A4=8DResult?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=92=8C=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在Combine方法中添加对Bottom状态的检查和处理 - 使用IfSucc方法简化成功状态下的值添加逻辑 - 修正泛型绑定示例中的语法错误 - 在FromNullable和FromNullableStruct方法中使用nameof获取参数名称 - 改进Exception属性的文档说明并优化异常消息 - 在Try方法中添加对空函数的验证 --- GFramework.Core/extensions/ResultExtensions.cs | 11 +++++++---- GFramework.Core/functional/Result.T.cs | 11 +++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/GFramework.Core/extensions/ResultExtensions.cs b/GFramework.Core/extensions/ResultExtensions.cs index dd806ed..0565a96 100644 --- a/GFramework.Core/extensions/ResultExtensions.cs +++ b/GFramework.Core/extensions/ResultExtensions.cs @@ -46,8 +46,11 @@ public static class ResultExtensions if (result.IsFaulted) return Result>.Fail(result.Exception); + if (result.IsBottom) + return Result>.Fail(new InvalidOperationException("Cannot combine Bottom results")); + if (result.IsSuccess) - values.Add(result.Match(succ: v => v, fail: _ => throw new InvalidOperationException())); + result.IfSucc(values.Add); } return Result>.Succeed(values); @@ -101,7 +104,7 @@ public static class ResultExtensions /// var result = Result<int>.Succeed(42); /// var bound = await result.BindAsync(async x => /// await GetUserAsync(x) is User user - /// User>.Succeed(user) + /// ? Result<User>.Succeed(user) /// : Result<User>.Fail(new Exception("User not found"))); /// /// @@ -262,7 +265,7 @@ public static class ResultExtensions string errorMessage = "Value is null") where T : class => value is not null ? Result.Succeed(value) - : Result.Fail(new ArgumentNullException(errorMessage)); + : Result.Fail(new ArgumentNullException(nameof(value), errorMessage)); /// /// 将可空值类型转换为 Result @@ -272,7 +275,7 @@ public static class ResultExtensions string errorMessage = "Value is null") where T : struct => value.HasValue ? Result.Succeed(value.Value) - : Result.Fail(new ArgumentNullException(errorMessage)); + : Result.Fail(new ArgumentNullException(nameof(value), errorMessage)); #endregion } \ No newline at end of file diff --git a/GFramework.Core/functional/Result.T.cs b/GFramework.Core/functional/Result.T.cs index 62bd2ef..a65c505 100644 --- a/GFramework.Core/functional/Result.T.cs +++ b/GFramework.Core/functional/Result.T.cs @@ -101,11 +101,17 @@ public readonly struct Result : IEquatable>, IComparable> public bool IsBottom => _state == ResultState.Bottom; /// - /// 获取内部异常,若为 Bottom 状态则抛出 InvalidOperationException + /// 获取内部异常: + /// - 若为 Failure 状态,则返回内部异常 + /// - 若为 Bottom 状态,则返回带有 "Result is in Bottom state." 消息的 InvalidOperationException + /// - 若为 Success 状态,则返回带有 "Cannot access Exception on a successful Result." 消息的 InvalidOperationException /// [Pure] public Exception Exception => _exception - ?? new InvalidOperationException("Result is in Bottom state."); + ?? (IsBottom + ? new InvalidOperationException("Result is in Bottom state.") + : new InvalidOperationException( + "Cannot access Exception on a successful Result.")); // ------------------------------------------------------------------ 取值 @@ -266,6 +272,7 @@ public readonly struct Result : IEquatable>, IComparable> /// 执行结果 public static Result Try(Func f) { + ArgumentNullException.ThrowIfNull(f); try { return new Result(f());