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

Task code requries additional type annotations (was : Runtime cast failure using tasks in F# 6.0 ) #12188

Closed
Lanayx opened this issue Sep 25, 2021 · 2 comments

Comments

@Lanayx
Copy link
Contributor

Lanayx commented Sep 25, 2021

Please provide a succinct description of the issue.

Repro steps

open System.Threading.Tasks

let myFunction (f, i) =
    task {
        do! f ""
        return ()
    }

[<EntryPoint>]
let main argv =
    let myTuple = (fun s -> Task.FromResult()), 1
    (myFunction myTuple).Wait()
    
    0

Expected behavior

The programs runs to end successfully. This code worked with Fsharp 5.0.1 and TaskBuilder.fs

Actual behavior

With Fsharp.Core 6.0 it fails in runtime with error

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Tuple`2[Microsoft.FSharp.Core.FSharpFunc`2[System.String,System.Threading.Tasks.Task`1[Microsoft.FSharp.Core.Unit]],System.Int32]' to type 'S
ystem.Tuple`2[Microsoft.FSharp.Core.FSharpFunc`2[System.String,System.Threading.Tasks.Task`1[Microsoft.FSharp.Core.Unit]],System.Object]'.

Known workarounds

It works when f or i parameter has type annotation

Related information

Provide any related information (optional):

  • Windows 10
  • Microsoft (R) Build Engine version 17.0.0-preview-21460-01+8f208e609 for .NET
  • Jetbrains Rider
@dsyme dsyme changed the title Runtime cast failure after updating to Fsharp.Core 6.0 Runtime cast failure using tasks in F# 6.0 Sep 27, 2021
@dsyme dsyme changed the title Runtime cast failure using tasks in F# 6.0 Task code requries additional type annotations (was : Runtime cast failure using tasks in F# 6.0 ) Sep 27, 2021
@dsyme
Copy link
Contributor

dsyme commented Sep 27, 2021

Thanks for this bug report. I've changed the title.

The underlying problem here is the use of SRTP for "task-like" constructs in F# 6.0, which is hitting problems associated right back to the mis-inclusion of #1650 into F#. I will find a suitable fix for this and we will also be adjuting SRTP to fully remove #1650 in F# vNext, one way or another.

It will take a while for that fix to come through the works. In the meantime you should indeed add type annotations to places where the types of tasks being executed by do! or let! is not simply determined. For example

let myFunction (f: string -> Task<_>, i) =
    task {
        do! f ""
        return ()
    }

@dsyme
Copy link
Contributor

dsyme commented Sep 30, 2021

Fixed by #12195

@dsyme dsyme closed this as completed Sep 30, 2021
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