When creating UDFs with F#, the flexible type inference might lead to function signatures that are not supported by Excel-DNA, or lead to unexpected results.

let MakeTwo x = 2 

This doesn't work (the UDF doesn't get registered) since the inferred type is 'a -> int, so is generic over the argument. This is equivalent to the C# signature:

public int MakeTwo<T>(T input) = { return 2; }
However, the following, with explicit typing, does work:

let MakeTwo (x : float) = 2 

This would apply to any function that is generic over its input. Another example is:

let AddString x y = x.ToString() + y.ToString()

which is of the type a' -> b' -> string and doesn't get exposed as an UDF either.

Adding explicit types removes the generic parameters:

let AddString (x:obj) (y:obj) = x.ToString() + y.ToString()

Even the simple example in the distribution can be a concern:

let Add x y = x + y 
F# infers this function to be of the type int -> int -> int, and if called in Excel as =Add(2.5,3.5) then this function will return 7 not 6.

let Add (x:float) (y:float) = x + y 

Last edited Nov 18, 2012 at 6:17 AM by govert, version 1


No comments yet.