rspeele / TaskBuilder.fs

F# computation expression builder for System.Threading.Tasks
Creative Commons Zero v1.0 Universal
235 stars 27 forks source link

Intended way to call async and ignore result #26

Closed jbtule closed 5 years ago

jbtule commented 5 years ago

do! isn't happy with a Task<_> and let! _ = can be a work around, but not very readable.

FSharp Async as Async.Ignore. But I don't see an equivalent with this library, and it seems like there should be.

Since Task<_> inherits from Task, I'm thinking

let Ignore (resultTask:Task<_>): Task = upcast resultTask

should work. But we are in the middle of porting some C# to F# code, and haven't tested it other than type checking wise it appears to work.

I was wondering if this is an issue anyone has dealt with.

Thanks,

rspeele commented 5 years ago

I just use let! _ = in my own code. It does look a little odd but in my view, it is equally or slightly more clear what's happening in let! _ = someTask compared to do! Task.Ignore(someTask). You can't end a task { } block with let! _ = ..., but in my mind it reads fine to just add another line of return (). Or just write do! ... :> Task.

That said, if you prefer to use a wrapper function in your codebase there is nothing wrong with doing so.

jbtule commented 5 years ago

Yeah the wrapper function did work for us, do! ... |> Task.Ignore but I kinda like the do! ... :> Task too.

module Task =
    let Ignore (resultTask:Task<_>): Task = upcast resultTask

Thanks!