Skip to content

Proposal: AsMaybeAsync #528

Open
Open

Description

@mollsju

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();

Activity

  1. vkhorikov commented on Jan 27, 2024

    @vkhorikov
    Owner

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

  2. RoelantM commented on Sep 18, 2024

    @RoelantM

    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?

  3. vkhorikov commented on Sep 28, 2024

    @vkhorikov
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions