Re:Show HN: Twenty
by Lawtonfogle on Thursday May 14, 2015 @10:13AM
(#9545233)
Am I correct in my understanding that with a closure, it is one or more data bound to one function, while with object orientation, one or more functions are bound to one or more data?
Yes, but it can go a bit further.
http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent
A singular closure is 1+ data bound to an entity (function). A singular object is multiple data/functions bound to one entity (object).
In the link above there are examples of either generating multiple closures tied to the same set of data (like tel's example), and also examples of having a singular closure that takes as a first parameter a "method" name and branching based on that. Using either solution you have an OO system using closures as your basis.
If you truly want to understand how it works then you'd probably want to write a compiler that supports closures; you'd then need to cover issues like how to allocate functions on the heap, the difference between locally allocated (typically on the stack) data bound to functions and globally allocated (typically on the heap) data bound to functions.
The second part to closures is understanding the theory, or the why and the high level functionality that it enables, such as callbacks with state or interactions with the likes of recursive (y-combinator like) functions.
EDIT: minor grammar fix