Skip to content

Added overload to switch method for use with async code.#106

Open
bwarthur wants to merge 1 commit into
mcintyre321:masterfrom
bwarthur:switch-async-overload
Open

Added overload to switch method for use with async code.#106
bwarthur wants to merge 1 commit into
mcintyre321:masterfrom
bwarthur:switch-async-overload

Conversation

@bwarthur

Copy link
Copy Markdown

Fixes #84.

I went with doing an overload of Switch instead of a separate SwitchAsync because I think it will provide a better default experience. As soon as someone adds async to each of the actions, it will switch over to the Func<> overload instead of relying on someone to know to go look for an explicitly named Async version.

@adnang

adnang commented Apr 20, 2022

Copy link
Copy Markdown

Will this result in IDE's highlighting that the Switch method is now async, and requires an await?

The problem i tried to highlight in the original issue wasn't with dicovery of the async method, but rather its usage - IMO, developers will still forget to call await on the method if its not explicit when there's a non-async overload.

Having an async overload without the Async suffix is quite unusual for stable .NET libraries, I think it will cause more confusion when the return type has changed to a Task without the method name changing.

This SO post has a similar belief https://stackoverflow.com/a/47810905

@mcintyre321 what do you think?

Comment thread Generator/Program.cs
}}

#if !NET35
public Task Switch({RangeJoined(", ", e => $"Func<T{e}, Task> f{e}")})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this to SwitchAsync.

Comment thread Generator/Program.cs
throw new InvalidOperationException();
}}

#if !NET35

@ChristopherHaws ChristopherHaws Dec 15, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be (!NETFRAMEWORK || NET40_OR_GREATER) instead of !NET35

@fbouvard

Copy link
Copy Markdown

Hi, would love to see this PR merged

@vflame

vflame commented Jul 7, 2023

Copy link
Copy Markdown

How does this compare to doing:

await oneOf.Match<Task>( t1 => Task.CompletedTask, t2 => Task.CompletedTask);

@fbouvard fbouvard mentioned this pull request Jan 23, 2024
@baynezy

baynezy commented Feb 2, 2024

Copy link
Copy Markdown

I badly need this. Any chance this happens?

@md-redwan-hossain

Copy link
Copy Markdown

Is this ever gonna merged?

@mcintyre321

mcintyre321 commented Feb 29, 2024 via email

Copy link
Copy Markdown
Owner

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 this pull request may close these issues.

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

9 participants