Skip to content

Instantly share code, notes, and snippets.

@in-async
Last active January 14, 2022 08:48
Show Gist options
  • Save in-async/813a7f0cb3407c09ba79e0e9ae479839 to your computer and use it in GitHub Desktop.
Save in-async/813a7f0cb3407c09ba79e0e9ae479839 to your computer and use it in GitHub Desktop.
式ブロック的なヘルパー。
using System;
namespace Commons {
/// <remarks>
/// cf. Proposal: Expression blocks https://github.com/dotnet/csharplang/issues/3086
/// cf. Proposal: Sequence Expressions https://github.com/dotnet/csharplang/issues/377
/// </remarks>
public static class ExpressionFunc {
public static T With<T>(this T obj, Action<T> action) {
if (action is null) { throw new ArgumentNullException(nameof(action)); }
action(obj);
return obj;
}
public static TResult To<T, TResult>(this T obj, Func<T, TResult> func) {
if (func is null) { throw new ArgumentNullException(nameof(func)); }
return func(obj);
}
public static void With(Action action) {
if (action is null) { throw new ArgumentNullException(nameof(action)); }
action();
}
public static TResult To<TResult>(Func<TResult> func) {
if (func is null) { throw new ArgumentNullException(nameof(func)); }
return func();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment