Tail calls in F#
blogs.msdn.com
blogs.msdn.com
Reading that just kind of made me break out into a cold sweat. I imagine I will be looking at some IL tonight because of this.
I still don't quite get the reason for why try-catch or try-finally blocks cannot have a tail call in them?
But if the tail call is in a try-finally block the "tail call" is not actually the tail. There is more code that needs to be run after the execution of the tail call. The code has to return back from the tail call and execute the finally code.
What you thought was a tail call is actually no longer a tail call at all.
Yes, that statement is correct and what we're talking about.
Having a try-finally in the body of the tail-call function shouldn't make a difference.
To be clear:
try {
...
tailcall(...);
}
finally { ... }
That could block tail call optimization.On the other hand, tail-calls can be optimized into a while loop, and the F# compiler does this in some cases (check out the IL it produces), so then you could have a recursive tail-call within a try/finally there, it just wouldn't be using the 'tail.call' / 'tail.callvirt' / 'tail.calli' OpCode.
try
let rec func ... =
...
func( ... )
let result = func( ... )
with
| ex -> ...
I don't know exactly how I ended up thinking that, seems a bit silly in retrospect.I'm not sure about try/catch without the finally, perhaps there is some error check that occurs in the IL after the body of the try block?
http://stackoverflow.com/questions/7538584/f-vs-ocaml-stack-...