(FL0034): ReimplementsFunction shown error, but after implementing suggestion build fails
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
- Have C# class:
public class FuncClass
{
public FuncClass(Func<string, string> func)
{
Func = func;
}
public Func<string, string> Func { get; }
}
- Have local function as string -> string:
let myLocalFunc str = sprintf "%s" str - 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))
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.