question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage and meaning of Tap extension method

See original GitHub issue

We’re using CSharpFunctionalExtensions package in our projects more than a year and we appreciated the changes of many OnSuccess methods to separate methods with different meaning, such as Tap, Bind etc.

After some using our team got an intuitive understanding of when to use each of them and we wrote not totally functional, but easy-readable and clear constructions such as:

await GetAvailability(request, languageCode)
                    .Bind(ConvertCurrencies)
                    .Map(ApplyMarkups)
                    .Map(Convert)
                    .Tap(SaveResult)

When I read these lines I know that GetAvailability returns a Result<T1>, then passes it to the ConvertCurrencies, which returns result of another type (Result<T2>) and so on. When I see a usage of Tap it looks like a method that doing nothing with returning result, just doing some job with no result (or having result as a Task). And as I understand, Tap has the same meaning in other languages.

But during one of the code reviews we’ve found out that there are some Tap overloads that do change the returning result and this was totally surprising:

/// <summary>
///     If the calling result is a success, the given function is executed and its Result is checked. If this Result is a failure, it is returned. Otherwise, the calling result is returned.
/// </summary>
public static async Task<Result<T, E>> Tap<T, K, E>(this Task<Result<T, E>> resultTask, Func<T, Result<K, E>> func)
{
     Result<T, E> result = await resultTask.DefaultAwait();
     return result.Tap(func);
}

Could you tell why it was done in such way and what is the new meaning of the Tap in terms of the library?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:30 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
hankovichcommented, Nov 25, 2020

@mingazhev ah I see your point, Name is confusing indeed 😃

Several days ago I was discussing Taps and TapIfs with one of my colleagues and he admitted that he does not understand code with Taps exactly because of the different semantics.

1reaction
hankovichcommented, Nov 30, 2020

@mingazhev I think you should create a fork first, commit/push your changes and open a PR then. This guide covers all the steps : https://github.com/MarcDiethelm/contributing/blob/master/README.md

Read more comments on GitHub >

github_iconTop Results From Across the Web

"tap" extension method?? : r/csharp
Ive seen an extension method on the object type that goe's like myClass.tap(t => t.someProperty = 42); It takes a delegate and applies...
Read more >
Extension Methods in CSharp with Real-time Examples
At the end of this video, you will understand what exactly Extension Methods are and when and how to use these extension methods...
Read more >
Task asynchronous programming model
Learn when and how to use Task-based async programming, a simplified approach to asynchronous programming in C#.
Read more >
Extension methods
Extension methods add functionality to existing libraries. You might use extension methods without even knowing it. For example, when you use code completion ......
Read more >
C# Extension Method
An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found