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