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

Proposal: AsMaybeAsync #528

Open
mollsju opened this issue Jan 24, 2024 · 3 comments
Open

Proposal: AsMaybeAsync #528

mollsju opened this issue Jan 24, 2024 · 3 comments

Comments

@mollsju
Copy link

mollsju commented Jan 24, 2024

Hello!
Any chance AsMaybeAsync could be added, just as we have ToResultAsync?

It would allow for less clutter when combined with EF.

Current:
Maybe<Entity> maybeEntity = (await dbContext.Entity.FirstOrDefaultAsync(x => x.Id == EntityId)).AsMaybe();

With AsMaybeAsync:
Maybe<Entity> maybeEntity = await dbContext.Entity.FirstOrDefaultAsync(x => x.Id == EntityId).AsMaybeAsync();

@vkhorikov
Copy link
Owner

Yes, looks good. Feel free to submit a PR please.

@RoelantM
Copy link

RoelantM commented Sep 18, 2024

perhaps include a implicit operator as well? i was working on a simular solution/problem.
Where I am calling a function that returns Result<T, TE>. But my method signature should return a Maybe. So in the case that result.isFailure it needs to convert. Now I have:

 public static async Task<Maybe<T>> ToMaybeAsync<T, TE>(this Task<Result<T, TE>> task) where T : class
 {
     var result = await task.ConfigureAwait(false);
     return result.IsSuccess ? Maybe<T>.From(result.Value) : Maybe<T>.None;
 }

But i guess the implicit converter would even be better.

 public static class MaybeExtensions
{
    // Implicit conversion operator from Result<T, TE> to Maybe<T>
    public static implicit operator Maybe<T>(Result<T, TE> result)
    {
        return result.IsSuccess ? Maybe<T>.From(result.Value) : Maybe<T>.None;
    }
}

I think i cannot add this directly in my code, can I?

@vkhorikov
Copy link
Owner

The lack of the implicit conversion is intentional. Check out this article: https://khorikov.org/posts/2021-11-08-converting-result-to-maybe/

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

3 participants