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 AsyncMethodBuilderAttribute to async monads (EitherAsync, OptionAsync, etc) #1019

Closed
fjmorel opened this issue Apr 12, 2022 · 4 comments
Closed

Comments

@fjmorel
Copy link

fjmorel commented Apr 12, 2022

Refactoring methods to return EitherAsync<L, R> rather than Task<Either<L, R>> can be a challenge sometimes since you can't use async/await. This is the error if you try to use async with EitherAsync as the return type (emphasis mine):

The return type of an 'async' method must be a 'void', 'Task', 'Task', a task-like type, 'IAsyncEnumerable', or 'IAsyncEnumerator'

Based on https://devblogs.microsoft.com/premier-developer/extending-the-async-methods-in-c/#task-like-types I believe it would be possible to update OptionAsync, EitherAsync, etc to be usable as return types of async methods.

I noticed recently that I can now await these types (thank you!), so this would be an extra thing that makes them easier to use.

edit: more discussion here: #960

@fjmorel
Copy link
Author

fjmorel commented Apr 12, 2022

If this is desirable, I'd be willing to try implementing it myself.

@louthy
Copy link
Owner

louthy commented Apr 12, 2022

This already exists for OptionAsync, but not for EitherAsync. This approach doesn't work for types with multiple generic arguments, because the C# team's hack to make async work through the god-awful attributes and GetAwaiter will only look for types with a single generic parameter.

@fjmorel
Copy link
Author

fjmorel commented Apr 12, 2022

Oh, I didn't know that. 😢

Do you happen to know if the compiler team is aware of that? I could file or upvote an issue in dotnet/roslyn

@fjmorel
Copy link
Author

fjmorel commented Apr 12, 2022

Found it, which you already commented on: dotnet/csharplang#3723

@fjmorel fjmorel closed this as completed Apr 12, 2022
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

No branches or pull requests

2 participants