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

Result.Try ambiguous invocation when using async statement lambdas since 3.11 #179

Open
DAHAG-ArisNourbakhsh opened this issue Mar 6, 2023 · 2 comments

Comments

@DAHAG-ArisNourbakhsh
Copy link

DAHAG-ArisNourbakhsh commented Mar 6, 2023

Before 3.11.0, these worked and compiled

var resultTask = Result.Try(async () =>
{
    await Task.CompletedTask;

    return new object();
});

var resultTask2 = Result.Try(async () =>
{
    await Task.CompletedTask;
});

but with 3.11.0 onwards I get a CS0121

The call is ambiguous between the following methods or properties: 'Result.Try(Func<Task>, Func<Exception, IError>)' and 'Result.Try(Func<ValueTask>, Func<Exception, IError>)'

I can of course just not do any async statement lambdas and put the code into dedicated methods but for me this feel a bit verbose and makes quick "try async thing inline" inside methods not so quick

static Task<Result> Foo(string input)
{
    return Result.Try(() => FooInternal(input));
}

static async Task FooInternal(string input)
{
    //Do something with the input
    await Task.CompletedTask;
}

instead of

static Task<Result> Foo(string input)
{
    return Result.Try(async () =>
    {
        //Do something with the input
        await Task.CompletedTask;
    });
}

Only 8 months too late with this feedback and honestly I wouldn't even have a good suggestion on how one would give ValueTask + Task support in overloads. I tried to think of any Microsoft written API that receive a Task and return a Task but couldn't think of anything similar.

Btw love your library

@saborrie
Copy link

A similar issue in Xunit: xunit/xunit#2808

@dguisinger
Copy link

With C# 11 and beyond you can specify the lambda return type:

await Result.Try(async 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

No branches or pull requests

3 participants