Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The difference is in the ownership and lifetime of the state used by the function. A pure function uses state that it owns or is not outlived by the mutable state it uses. Closures on the other hand uses that it doesn't own or is outlived by the state during the invocation.

The important detail here is that closures automatically moves the variable that lives in the stack frame into the heap. You can't do that with a regular function. In languages with manual memory management like C, if you try to store and use a pointer to a invalidated stack frame, you will either get a crash if you are lucky, or get a crazy bug that shouldn't happen within reason.

Your supporting and concluding statements are directed at a scarecrow though. The person you are replying to isn't contending that closures and objects are different, they are specifically saying that closures and (regular) functions has an important distinction, which I agree.

Although I would say the person you are replying to did have poor imprecise wording on their part.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: