rspeele / TaskBuilder.fs

F# computation expression builder for System.Threading.Tasks
Creative Commons Zero v1.0 Universal
235 stars 27 forks source link

Please elaborate a bit on "Tail calls are not optimized" #10

Closed pkese closed 6 years ago

pkese commented 6 years ago

The main Readme file says "Tail calls are not optimized".

However it is written more in a defensive manner of restricting responsibility or covering one's back for the situation that could not be solved. It is indeed a good practice to warn that something is not expected to work.

It's not clear however what does work or how to make a workaround for stuff that doesn't.

I would propose to include an example of how a recursive or loop function would have to be written in order not to cause stack or heap overflow in case where there is some main endless async loop function (e.g. something akin to code below):

let asyncApp state = task {
    let! event = waitForNextEventAsync()
    let! newState = updateStateAsync state event
    return asyncApp newState
}

asyncApp initialState

From what's written in Readme, I assume that the above code would leak, but don't really know how to fix it, e.g. would mutable state and a while do loop solve it? What about return, is it necessary at all..

Could you please provide a similar example of code that works and of code that doesn't work with a short explanation of what's actually going on.

rspeele commented 6 years ago

Done, see new README.

pkese commented 6 years ago

Perfect! Thanks.