Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a .SwitchAsync method which behaves as .Match returning a Task #84

Open
adnang opened this issue May 6, 2021 · 2 comments · May be fixed by #106
Open

Add a .SwitchAsync method which behaves as .Match returning a Task #84

adnang opened this issue May 6, 2021 · 2 comments · May be fixed by #106

Comments

@adnang
Copy link

adnang commented May 6, 2021

I've seen too many times developers using .Switch instead of .Match when calling async void code inside the lambdas, because the default behaviour when you want to perform a void operation is to .Switch rather than .Match, resulting in

OneOf<T1, T2> res = GetResult();

res.Switch(async t1 => await ProcessT1(t1), async t2 => await ProcessT2(t2));

which isn't awaited correctly (resulting in exceptions being lost)

A .SwitchAsync method which accepts a Func<T, Task> for each parameter would make it really clear than an async option can be used (even gives an IDE hint), and the behaviour would be the same as .Match(Func<T, Task>)

@mcintyre321
Copy link
Owner

Interesting... ware your thoughts on overloading on Switch (without the Async suffix) i.e.

public void Switch(Action<T0> f0, Action<T1> f1) { ... }
public Task Switch(Func<T0, Task> f0, Func<T1, Task> f1) { ... }

@adnang
Copy link
Author

adnang commented Jun 9, 2021

because the non-async overload is the original and the default for most current usages, i would make the Async part explicit. Omitting async is cleaner when the async version is the default, but omitting it here could still result in the same issue with developers forgetting to await the resulting task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants