Skip to content

Partial application doesn't work well with implicit cast #19200

@Lanayx

Description

@Lanayx

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

Repro steps

Provide the steps required to reproduce the problem:

open System

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

let test1 (f: Func<unit, int>) =
    fun arg ->
        f.Invoke(arg)

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

[<EntryPoint>]
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:

FSharpFunc<Unit, int> invoker1 = (FSharpFunc<Unit, int>) new Program.invoker1@19-1(new Func<Unit, int>(Program.partial()));
FSharpFunc<Unit, int> invoker2 = (FSharpFunc<Unit, int>) 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:

FSharpFunc<Unit, int> invoker1 = (FSharpFunc<Unit, int>) new Program.invoker1@19-1(new Func<Unit, int>((object) null, __methodptr(Invoke)));
FSharpFunc<Unit, int> invoker2 = (FSharpFunc<Unit, int>) new Program.invoker2@20(Program.partial());

Known workarounds

Extract partial applied function to the variable:

let x = partial ()
let invoker1 = test1 x

Related information

Provide any related information (optional):

  • .NET SDK 10.0.101

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions