Tuesday, November 24, 2009

Something to know when using F# Interactive for testing code.

I recently discovered a behaviour with F# Interactive when I was testing som code.
And it took me a while to understand why and solve the problem.
I will keep the example small but the code I was testing was mutch bigger than this.

Lets start with three simple functions.










Compile the functions and send them to F# Interactive and the result is this.





As expected the funcA takes an int as parameter and returns an int.
And funcB returns the value 100 which is the result from funcA with parameter 10 and funcC returns the value 400 which is the return value from funcA with parameter 20.

OK, no problem yet.
But now change funcA to this.





F# Interactive respond with the same function signature as before.




Then compile funcB again and sent it to F# Interactive and the result is this.





The value from funcB is expected as it returns the value from the new compiled funcA with parameter 10 (10*10*10 = 1000).

And now to the problem.
Now execute funcC without recompiling it and the result is this.




Function funcC is still using the first version of funcA thet return the value 400 with parameter 20.

It turns out that you always have to recompile all functions that uses a changed function in order to receive the right value back.

This example is simple but imagine you have several calls to a changed function then it is easy to miss one or a few of the calls to it.
This is what happend to med, I had about twenty calls to a function which I changed and I miss a few of them so I was spending some time to loclaize where the problem was.

If yoy compile a console application or a library then this problem never occurs.