Adventures in F#–Function Type Inference

Consider this F#:

let Reverse a b = b a

This means: given a and b (whatever they may be) call b as a function with 'a' as the argument. When I compile this and look at it under reflector I get:

    public static U Reverse<T, U>(T a, FastFunc<T, U> b)
        return b.Invoke(a);

Its interesting to me that F# is able to contextually deduce that b must be a function call.

That FastFunc<T,U> is not a delegate but it seems to represent a similar concept. I'm not sure why its not a delegate and this seems to limit interoperability with languages (like C#) which use delegates to represent this concept, note to self to investigate this later.

Another thought: That FastFunc is an abstract class means that the implementation of the function need not exist until its called. I don't think this is possible with straight delegates.

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments (6)

  1. That’s how the pipeline operator is defined:

    let (|>) x f = f x

    then you can do

    myThings |> ….

    This is pretty much what C# extension methods accomplish, albeit this way is more useful since it can operate on all methods :).

  2. Gilles Michard says:

    Is not FastFunc a substitute for the missing UnicastDelegate class? The choice of supporting only MulticastDelegate in the Framework for functions seems to be the problem.

  3. Jomo Fisher– Easily my favorite feature of F# so far is the combination of discriminated union and pattern

  4. Hi, as far as I know the reason why F# doesn’t use delegates internally is that they are slower (and this is an importan problem in a language where you manipulate with function all the time). However, when writing a public interface for an F# library (that should be "C# compatible") you can (should) use delegates which are also supported in F#.

  5. Jomo Fisher–This is the next part of the open-notebook series documenting my exploration of F#. Today,

  6. Jomo Fisher–This is the next part of the open-notebook series documenting my exploration of F#. Today

Skip to main content