mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(control): 修改Match方法返回Option类型
- 将Match方法的返回类型从TResult改为Option<TResult> - 匹配成功时返回Option<TResult>.Some(handler(value)) - 匹配失败时返回Option<TResult>.None()而不是抛出异常 - 移除InvalidOperationException异常抛出逻辑
This commit is contained in:
parent
20bbf2297e
commit
20dcb87def
@ -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;
|
||||
|
||||
/// <summary>
|
||||
@ -26,18 +29,20 @@ public static class ControlExtensions
|
||||
/// <param name="cases">匹配案例数组,每个包含谓词和处理器</param>
|
||||
/// <returns>匹配到的处理结果</returns>
|
||||
/// <exception cref="InvalidOperationException">当没有匹配的案例时抛出</exception>
|
||||
public static TResult Match<TSource, TResult>(
|
||||
public static Option<TResult> Match<TSource, TResult>(
|
||||
this TSource value,
|
||||
params (Func<TSource, bool> predicate, Func<TSource, TResult> handler)[] cases)
|
||||
{
|
||||
foreach (var (predicate, handler) in cases)
|
||||
{
|
||||
if (predicate(value))
|
||||
return handler(value);
|
||||
return Option<TResult>.Some(handler(value));
|
||||
}
|
||||
throw new InvalidOperationException("No matching case found");
|
||||
|
||||
return Option<TResult>.None();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// MatchOrDefault:带默认值的模式匹配
|
||||
/// </summary>
|
||||
@ -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<TSource, TSource> thenFunc,
|
||||
Func<TSource, TSource> elseFunc)
|
||||
=> predicate(value) ? thenFunc(value) : elseFunc(value);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// TakeIf:条件返回值或null
|
||||
/// </summary>
|
||||
@ -115,4 +121,4 @@ public static class ControlExtensions
|
||||
Func<TSource, bool> predicate)
|
||||
where TSource : class
|
||||
=> !predicate(value) ? value : null;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user