From 20dcb87defc3af99e5c6fd378e4a0a9ee0c725fd Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 31 Jan 2026 21:40:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(control):=20=E4=BF=AE=E6=94=B9Match?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=BF=94=E5=9B=9EOption=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将Match方法的返回类型从TResult改为Option - 匹配成功时返回Option.Some(handler(value)) - 匹配失败时返回Option.None()而不是抛出异常 - 移除InvalidOperationException异常抛出逻辑 --- .../functional/control/ControlExtensions.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/GFramework.Core/functional/control/ControlExtensions.cs b/GFramework.Core/functional/control/ControlExtensions.cs index 2e0ab2d..92ccd76 100644 --- a/GFramework.Core/functional/control/ControlExtensions.cs +++ b/GFramework.Core/functional/control/ControlExtensions.cs @@ -10,6 +10,9 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +using GFramework.Core.functional.types; + namespace GFramework.Core.functional.control; /// @@ -26,18 +29,20 @@ public static class ControlExtensions /// 匹配案例数组,每个包含谓词和处理器 /// 匹配到的处理结果 /// 当没有匹配的案例时抛出 - public static TResult Match( + public static Option Match( this TSource value, params (Func predicate, Func handler)[] cases) { foreach (var (predicate, handler) in cases) { if (predicate(value)) - return handler(value); + return Option.Some(handler(value)); } - throw new InvalidOperationException("No matching case found"); + + return Option.None(); } + /// /// MatchOrDefault:带默认值的模式匹配 /// @@ -57,6 +62,7 @@ public static class ControlExtensions if (predicate(value)) return handler(value); } + return defaultValue; } @@ -89,7 +95,7 @@ public static class ControlExtensions Func thenFunc, Func elseFunc) => predicate(value) ? thenFunc(value) : elseFunc(value); - + /// /// TakeIf:条件返回值或null /// @@ -115,4 +121,4 @@ public static class ControlExtensions Func predicate) where TSource : class => !predicate(value) ? value : null; -} +} \ No newline at end of file