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 · 7 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 () => { ... })

@altmann altmann added the v3.16 label Jun 19, 2024
@altmann altmann removed the v3.16 label Jun 30, 2024
@kmc059000
Copy link

In xunit/xunit#2808, by recommendation from the Roslyn team, they removed all overloads that took a ValueTask.

@altmann
Copy link
Owner

altmann commented Jul 29, 2024

Interesting. Who used ValueTasks in combination with FluentResults?

I will search for the inital issue to introduce these ValueTask methods and then we can make a decision.

@saborrie
Copy link

saborrie commented Aug 4, 2024

Looks like both Task and ValueTask were added simultaneously here: 3bb9607, which was PR #146, no mention of ValueTask or even Task for that matter seems to be part of the discussion

@kmc059000
Copy link

@altmann @saborrie this comment on #146 alludes to #143 which added ValueTask support. Not a ton more context, but something.

@guillaumeagile
Copy link

Hello there,
we face the same issuen trying to "Try" with an Asynchronuous method
It does not compile:
Ambiguous invocation:   FluentResults. Result Try(System. Action, System. Func<System. Exception,FluentResults. IError>) (in class Result)  .......... Try(System. Func<System. Threading. Tasks. ValueTask>, System. Func<System. Exception,FluentResults. IError>) (in class Result) match

Even with await keyword before "Try" method, because this method is not asynch (pretty logical, though)

Do you consider writing a "TryAsync", to make the compiler happy ?

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

6 participants