FSharpLint icon indicating copy to clipboard operation
FSharpLint copied to clipboard

(FL0034): ReimplementsFunction shown error, but after implementing suggestion build fails

Open Grenkin1988 opened this issue 5 years ago • 1 comments

Description

Please provide a succinct description of your issue. I have a C# class with constructor accepting Func... as parameter. Creation of object in F# and using applied function as argument gives Lint error, but after fixing it based on suggestion code does not compile. In order to compile I need to instantiate instance of Func So, the question is: Is it really more preferable way of writing? FuncClass(fun str -> myLocalFunc str) - compile but Lint is angry FuncClass(myLocalFunc) - do not compile FuncClass(Func<_,_>(myLocalFunc)) - compile

Repro steps

  1. Have C# class:
public class FuncClass
    {
        public FuncClass(Func<string, string> func)
        {
            Func = func;
        }

        public Func<string, string> Func { get; }
    }
  1. Have local function as string -> string: let myLocalFunc str = sprintf "%s" str
  2. Create instance:
let createNotComplientClass () =
    FuncClass(fun str -> myLocalFunc str)

Expected behavior

Probably it is OK to have re-implemented function here

Actual behavior

Lint gives error, but suggested fix do not compile

Known workarounds

This do not give Lint issue, and compile. But is it best F# approach?

let createComplientClass () =
    FuncClass(Func<_,_>(myLocalFunc))

Grenkin1988 avatar May 18 '20 11:05 Grenkin1988

Thanks for the detailed report @Grenkin1988. This is definitely an issue. It's happening because C# style delegates only get automatically converted for lambda functions. I would say this is a valid use case and the linter shouldn't report an issue here.

We can try to update the rule to check if the parameter the lambda is being passed to is a delegate (System.Func or System.Action) and not report any issue in that case. However I am not seeing a way to differentiate between a delegate type and a normal F# function type in the F# type checker info.

In the meantime, you can suppress the rule.

jgardella avatar May 19 '20 18:05 jgardella