mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-24 20:34:29 +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.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
using GFramework.Core.functional.types;
|
||||||
|
|
||||||
namespace GFramework.Core.functional.control;
|
namespace GFramework.Core.functional.control;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -26,18 +29,20 @@ public static class ControlExtensions
|
|||||||
/// <param name="cases">匹配案例数组,每个包含谓词和处理器</param>
|
/// <param name="cases">匹配案例数组,每个包含谓词和处理器</param>
|
||||||
/// <returns>匹配到的处理结果</returns>
|
/// <returns>匹配到的处理结果</returns>
|
||||||
/// <exception cref="InvalidOperationException">当没有匹配的案例时抛出</exception>
|
/// <exception cref="InvalidOperationException">当没有匹配的案例时抛出</exception>
|
||||||
public static TResult Match<TSource, TResult>(
|
public static Option<TResult> Match<TSource, TResult>(
|
||||||
this TSource value,
|
this TSource value,
|
||||||
params (Func<TSource, bool> predicate, Func<TSource, TResult> handler)[] cases)
|
params (Func<TSource, bool> predicate, Func<TSource, TResult> handler)[] cases)
|
||||||
{
|
{
|
||||||
foreach (var (predicate, handler) in cases)
|
foreach (var (predicate, handler) in cases)
|
||||||
{
|
{
|
||||||
if (predicate(value))
|
if (predicate(value))
|
||||||
return handler(value);
|
return Option<TResult>.Some(handler(value));
|
||||||
}
|
}
|
||||||
throw new InvalidOperationException("No matching case found");
|
|
||||||
|
return Option<TResult>.None();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MatchOrDefault:带默认值的模式匹配
|
/// MatchOrDefault:带默认值的模式匹配
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -57,6 +62,7 @@ public static class ControlExtensions
|
|||||||
if (predicate(value))
|
if (predicate(value))
|
||||||
return handler(value);
|
return handler(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +95,7 @@ public static class ControlExtensions
|
|||||||
Func<TSource, TSource> thenFunc,
|
Func<TSource, TSource> thenFunc,
|
||||||
Func<TSource, TSource> elseFunc)
|
Func<TSource, TSource> elseFunc)
|
||||||
=> predicate(value) ? thenFunc(value) : elseFunc(value);
|
=> predicate(value) ? thenFunc(value) : elseFunc(value);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// TakeIf:条件返回值或null
|
/// TakeIf:条件返回值或null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -115,4 +121,4 @@ public static class ControlExtensions
|
|||||||
Func<TSource, bool> predicate)
|
Func<TSource, bool> predicate)
|
||||||
where TSource : class
|
where TSource : class
|
||||||
=> !predicate(value) ? value : null;
|
=> !predicate(value) ? value : null;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user