dotnet / dotnet/fsharp

`use` binding for `IAsyncDisposable` not supported in F# interactive

Open
#14,454 5 comments 1 reaction 0 assignees View on GitHub
AI-thinks-issue-fixed Area-SetupAndDelivery Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

`task` computation expressions do not support `use` or `use!` bindings for the `IAsyncDisposable` interface when used in F# interactive.

**Repro steps**

Make a type that implements `IAsyncDisposable` and bind it with a `use` or `use!` within a `task` CE:

```fsharp
open System
open System.Threading.Tasks

let asyncDisposable =
{ new IAsyncDisposable with
member _.DisposeAsync() =
printfn "Disposing async"
ValueTask() }

let t = task {
use d = asyncDisposable
return ()
}
```

**Expected behavior**

The code should compile.

**Actual behavior**

Error message in both the editor and F# interactive output window

![image](https://user-images.githubusercontent.com/3354932/206403270-c92aa6d8-38c5-4848-9e9f-73a2494548c2.png)

```
>>

Script1.fsx(11,11): error FS0001: The type 'IAsyncDisposable' is not compatible with the type 'IDisposable'
```

**Known workarounds**

Use a `let` binding and call `DisposeAsync` explicitly, although this is not possible within a `finally` block.

**Related information**

Compiles and runs without issue in regular `.fs` files.

* Operating system: Windows 11, 22H2
* .NET Runtime kind: .NET 7.0.100, F# Interactive version 12.4.0.0 for F# 7.0
* Visual Studio Professional, 17.4.2

```powershell
>> dotnet --info
.NET SDK:
Version: 7.0.100
Commit: e12b7af219

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100\

Host:
Version: 7.0.0
Architecture: x64
Commit: d099f075e4

.NET SDKs installed:
7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.