Skip to content

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

@Lanayx

Description

@Lanayx

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions