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 AsyncResult.ofTask with non-generic Task passed in #79

Closed
ysangkok opened this issue May 25, 2020 · 9 comments
Closed

Add AsyncResult.ofTask with non-generic Task passed in #79

ysangkok opened this issue May 25, 2020 · 9 comments

Comments

@ysangkok
Copy link

Methods like ConnectAsync returns a non-generic Task, which cannot be used with AsyncResult.ofTask

@TheAngryByrd TheAngryByrd added the good first issue Good for newcomers label May 25, 2020
@TheAngryByrd
Copy link
Collaborator

TheAngryByrd commented May 25, 2020

Sounds like a good addition!

@TheAngryByrd
Copy link
Collaborator

We have a function for that ofTaskAction .

@ysangkok
Copy link
Author

I tried that, this is the error I get:

Lightning.fs(237,94): 
error FS0001: The type 'unit' is not compatible with the type 'Task' [Backend.fsproj]
  
Lightning.fs(237,13): 
error FS0041: A unique overload for method 'Bind' could not be determined based on type information prior to this program point. A type annotation may be needed. 
Candidates: 
member AsyncResultBuilder.Bind : async':Async<'T> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>, 
member AsyncResultBuilder.Bind : asyncResult:Async<Result<'T,'TError>> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>, 
member AsyncResultBuilder.Bind : result:Choice<'T,'TError> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>, 
member AsyncResultBuilder.Bind : result:Result<'T,'TError> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>, 
member AsyncResultBuilder.Bind : task:Task * binder:(unit -> Async<Result<'T,'TError>>) -> Async<Result<'T,'TError>>, member AsyncResultBuilder.Bind : task:Task<'T> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>, 
member AsyncResultBuilder.Bind : taskResult:Task<Result<'T,'TError>> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>> [Backend.fsproj]

@ysangkok
Copy link
Author

let me prepare a complete example...

@ysangkok
Copy link
Author

ysangkok commented May 25, 2020

Actually, I get an error even with this:

namespace Lightning

open FsToolkit.ErrorHandling
module Lightning =

    let Test () =
        asyncResult {
            do! async { return (Ok "test") }
            return ()
        }

the error is

Lightning.fs(8,13): error FS0041: No overloads match for method 'Bind'. The available overloads are shown below. [Backend.fsproj]
  Possible overload: 'member AsyncResultBuilder.Bind : asyncResult:Async<Result<'T,'TError>> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'unit -> Async<Result<unit,'c>>'    is not compatible with type    'string -> Async<Result<'a,'b>>'    .
  Possible overload: 'member AsyncResultBuilder.Bind : taskResult:System.Threading.Tasks.Task<Result<'T,'TError>> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'Async<Result<string,'c>>'    is not compatible with type    'System.Threading.Tasks.Task<Result<'a,'b>>'    .
  Possible overload: 'member AsyncResultBuilder.Bind : result:Result<'T,'TError> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'Async<Result<string,'c>>'    is not compatible with type    'Result<'a,'b>'    .
  Possible overload: 'member AsyncResultBuilder.Bind : result:Choice<'T,'TError> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'Async<Result<string,'c>>'    is not compatible with type    'Choice<'a,'b>'    .
  Possible overload: 'member AsyncResultBuilder.Bind : task:System.Threading.Tasks.Task * binder:(unit -> Async<Result<'T,'TError>>) -> Async<Result<'T,'TError>>'. Type constraint mismatch. The type     'Async<Result<string,'a>>'    is not compatible with type    'System.Threading.Tasks.Task'    .
  Possible overload: 'member AsyncResultBuilder.Bind : task:System.Threading.Tasks.Task<'T> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'Async<Result<string,'b>>'    is not compatible with type    'System.Threading.Tasks.Task<'a>'    .
  Possible overload: 'member AsyncResultBuilder.Bind : async':Async<'T> * binder:('T -> Async<Result<'U,'TError>>) -> Async<Result<'U,'TError>>'. Type constraint mismatch. The type     'unit -> Async<Result<unit,'d>>'    is not compatible with type    'Result<string,'a> -> Async<Result<'b,'c>>'

Is this not compatible with F# 4.5?

@TheAngryByrd TheAngryByrd removed the good first issue Good for newcomers label May 25, 2020
@TheAngryByrd
Copy link
Collaborator

Hmm we have a test that does bind against a unit task.

testCaseAsync "Bind Task" <| async {
let innerData = "Foo"
let! actual = asyncResult {
do! Task.FromResult innerData :> Task
}
Expect.equal actual (Result.Ok ()) "Should be ok"

Is this not compatible with F# 4.5?

Version 1.2.6 requires FSharp 4.6.2.

@TheAngryByrd
Copy link
Collaborator

It doesn't look like there's a good reason to not drop the minimum required FSharp.Core version, I'll open a PR for that.

@TheAngryByrd
Copy link
Collaborator

namespace Lightning

open FsToolkit.ErrorHandling
module Lightning =

    let Test () =
        asyncResult {
            do! async { return (Ok "test") }
            return ()
        }

do! is invalid here since you're trying to bind against something that has data. In your case the value "Test". You have two options.

Ignore the result:

 let! _ = async { return Ok "test" }

or have the value be a unit:

do! async { return Ok () }

@ysangkok
Copy link
Author

Aaaah, thank you so much! It makes sense, I get it. Indeed I would appreciate if the min version was F# 4.5, since that is what the Mono packages for Ubuntu ships.

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