CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Add Tap* extensions to Maybe

Open marcinjahn opened this issue 8 months ago • 2 comments

I find the existing Execute/ExecuteNoValue extensions a bit inconvenient, due to:

  1. Naming that could just reuse the established Tap nomenclature (used in Result already)
  2. void return type prohibiting fluent method chaining.

With Execute the flow looks as follows:

var maybe = GetMaybe();

maybe.ExecuteNoValue(() => logger.LogWarning("There was no value"));

return maybe.Or(someDefaultValue);

With the new Tap, it could be:

return GetMaybe()
   .TapNoValue(() => logger.LogWarning("There was no value"))
   .Or(someDefaultValue);

I also added Obsolete attribute to Execute since I think it's not really needed when Tap is there. Please do let me know if that's alright.

The new code is really just a copy of existing Execute/ExecuteNoValue code with added return functionality. Tests are also mostly copied.

marcinjahn avatar Apr 12 '25 19:04 marcinjahn