diff --git a/CHANGELOG.md b/CHANGELOG.md index a8765e3..71ee6f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New features +- Added overloads of `Louis.Fluency.FluentExtensions.Invoke` that allow for additional arguments to be passed to lambdas to avoid the allocation of closure objects. +- Added method `Louis.Fluency.FluentExtensions.InvokeIf` that calls the provided action only if a condition is `true`. + ### Changes to existing features ### Bugs fixed in this release diff --git a/src/Louis/Fluency/FluentExtensions-Invoke.cs b/src/Louis/Fluency/FluentExtensions-Invoke.cs index d6730c8..f33b727 100644 --- a/src/Louis/Fluency/FluentExtensions-Invoke.cs +++ b/src/Louis/Fluency/FluentExtensions-Invoke.cs @@ -28,4 +28,108 @@ public static T Invoke(this T @this, Action action) action(@this); return @this; } + + /// + /// Invokes an action on an object and an additional argument and returns the same object. + /// + /// The type of the object. + /// The type of the additional parameter to . + /// The object on which this method was called. + /// The additional argument to pass tp . + /// The action to perform on and . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg arg, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, TArg3 arg3, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2, arg3); + return @this; + } + + /// + /// Invokes an action on an object and additional arguments and returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The type of the fourth additional parameter to . + /// The object on which this method was called. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The fourth additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Invoke(this T @this, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + action(@this, arg1, arg2, arg3, arg4); + return @this; + } } diff --git a/src/Louis/Fluency/FluentExtensions-InvokeIf.cs b/src/Louis/Fluency/FluentExtensions-InvokeIf.cs new file mode 100644 index 0000000..6684ba5 --- /dev/null +++ b/src/Louis/Fluency/FluentExtensions-InvokeIf.cs @@ -0,0 +1,160 @@ +// Copyright (c) Tenacom and contributors. Licensed under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using CommunityToolkit.Diagnostics; + +namespace Louis.Fluency; + +partial class FluentExtensions +{ + /// + /// Invokes an action on an object if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The object on which this method was called. + /// The condition to check. + /// The action to perform on . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this); + } + + return @this; + } + + /// + /// Invokes an action on an object and an additional argument if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The additional argument to pass tp . + /// The action to perform on and . + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg arg, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2, arg3); + } + + return @this; + } + + /// + /// Invokes an action on an object and additional arguments if a condition if satisfied, then returns the same object. + /// + /// The type of the object. + /// The type of the first additional parameter to . + /// The type of the second additional parameter to . + /// The type of the third additional parameter to . + /// The type of the fourth additional parameter to . + /// The object on which this method was called. + /// The condition to check. + /// The First additional argument to pass tp . + /// The second additional argument to pass tp . + /// The third additional argument to pass tp . + /// The fourth additional argument to pass tp . + /// The action to perform on and the additional arguments. + /// A reference to after returns. + /// + /// is . + /// - or - + /// is . + /// + public static T InvokeIf(this T @this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Action action) + { + Guard.IsNotNull(@this); + Guard.IsNotNull(action); + + if (condition) + { + action(@this, arg1, arg2, arg3, arg4); + } + + return @this; + } +} diff --git a/src/Louis/PublicAPI.Unshipped.txt b/src/Louis/PublicAPI.Unshipped.txt index 5de894d..336f9cf 100644 --- a/src/Louis/PublicAPI.Unshipped.txt +++ b/src/Louis/PublicAPI.Unshipped.txt @@ -86,7 +86,16 @@ static Louis.Fluency.FluentExtensions.IfElse(this T this, bool condition, Lou static Louis.Fluency.FluentExtensions.IfElse(this T this, bool condition, System.Action! then, System.Action! else) -> T static Louis.Fluency.FluentExtensions.IfNotNull(this T this, T1? arg, Louis.Fluency.FluentAction! then) -> T static Louis.Fluency.FluentExtensions.IfNotNull(this T this, T1? arg, System.Action! then) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, TArg3 arg3, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg1 arg1, TArg2 arg2, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Invoke(this T this, TArg arg, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Invoke(this T this, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, TArg3 arg3, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg arg, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, System.Action! action) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, System.Action? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T