I’m curious about the performance enhancements that have been made for FSharpFunc<_>.
Is it the fact that it does not contain multiple delegate so there is no need to loop over all the references when firing a function call ? Anything else ?
I’m curious about the performance enhancements that have been made for FSharpFunc<_> . Is
Share
I think that the primary motivation for using
FSharpFunc<>rather thanFunc<>or any other delegate is that you cannot create a class that would inherit from a delegate type (at first, this sounds reasonable, but in .NET, delegate is actually just some special class, so it may be in principle possible to allow this). Why is this needed?If you write a function in F# then it is (in a relatively few, but quite important cases) treated in a curried form. For example
int -> int -> intis actually a function typeint -> (int -> int)(currying means that you write a function using just functions of single parameter – if you call it with the first argument, you’ll get a function as a result and you can invoke the returned function with the second argument).If F# used delegates, the type would be something like
Func<int, Func<int, int>>. As Brian mentioned, the invocationf x ywould be translated into two invocations:f(x)(y). This kind of invocation is however the most common (specifying just a single argument is called partial function application). So, when F# compiles a function like this, it creates an inherited class with an optimized invoke method, so that it can be invoked asf.Invoke(x, y):Unfortunately, it isn’t possible to create such class by inheriting from standard
Func(because it is a delegate), so F# has to declare its own type which can be used as a base class…