dotnet / dotnet/fsharp

Partial application doesn't work well with implicit cast

Open
#19,200 2 comments 0 reactions 0 assignees View on GitHub
Bug
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Partial application doesn't work as expected when implicit cast to `Func<_>` takes place

**Repro steps**

Provide the steps required to reproduce the problem:

```fsharp
open System

let partial () =
let mutable acc = 0
fun () ->
acc <- acc + 1
acc

let test1 (f: Func) =
fun arg ->
f.Invoke(arg)

let test2 (f: unit -> int) =
fun arg ->
f arg

[]
let main argv =
let invoker1 = test1 (partial ())
let invoker2 = test2 (partial ())

[ invoker1 (); invoker1 (); invoker1 () ] |> printfn "%A"
[ invoker2 (); invoker2 (); invoker2 () ] |> printfn "%A"

0
```

**Expected behavior**

Output
```
[1; 2; 3]
[1; 2; 3]
```

Expected decompiled code:
```cs
FSharpFunc invoker1 = (FSharpFunc) new Program.invoker1@19-1(new Func(Program.partial()));
FSharpFunc invoker2 = (FSharpFunc) new Program.invoker2@20(Program.partial());
```

**Actual behavior**

Output
```
[1; 1; 1]
[1; 2; 3]
```
Partial application doesn't work correctly with invoker 1. Here is decompiled code:

```cs
FSharpFunc invoker1 = (FSharpFunc) new Program.invoker1@19-1(new Func((object) null, __methodptr(Invoke)));
FSharpFunc invoker2 = (FSharpFunc) new Program.invoker2@20(Program.partial());
```

**Known workarounds**

Extract partial applied function to the variable:
```fsharp
let x = partial ()
let invoker1 = test1 x
```

**Related information**

Provide any related information (optional):

* .NET SDK 10.0.101

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.