3 ms·
Closures are an implementation technique and objects a semantics. To equate them is to confuse implementation and interface. (Functions + tuples + recursion) i
by tel 12y ago
Closures are an implementation technique and objects a semantics. To equate them is to confuse implementation and interface.
(Functions + tuples + recursion) is powerful enough to embed objects very nicely though!
- jonahx 12y agohi tel, would you mind elaborating on what you mean by "objects [are] a semantics"?
- kd0amg 12y agoA closure is a data structure commonly used to represent a first-class function (containing a function pointer for the code and some sort of structure mapping the environment's bound variables to their values). Implementing a language with first-class functions does not require using closures. You could have an interpreter that performs beta reduction on the syntax tree, and first-class functions would still behave the same way. So closures are an implementation detail. In OOP, there are pieces of data that have operations associated with them. Two objects are free to have their own "version" of the same operation (same-named), and when you invoke it on an object, you get that object's version. In writing one of those operations, you have a handle on the thing you're operating on without having to take it as an argument. This specifies what result you'll compute and what side effects you'll cause by invoking an operation (i.e., the semantics). It does not specify things like where in program memory the code for that operation will live or how invoking it will work (implementation).
- agumonkey 12y agoYou need a term to distinguish non closing over closing functions, hence the closure leakage. Or maybe HOF ?
- deleted 12y ago[deleted]
- tel 12y agoFunctions that don't "close over the environment" are ones that are defined in the null environment. Normally you have Gamma, x : a |- e : b -------------------------- Gamma |- \x -> e : b But you could also have "bare functions" if you liked x : a |- e : b ---------------------- . |- \x -> e : b This would force all names to be explicitly applied, I think.
- agumonkey 12y agoThe use of 'closure' is an idiomatic shortcut to express that.
- tel 12y agoSort of. It's also a didactic method to talk about it and it often is quickly followed with people talking about stack pointers or environment frames. You can blur the lines if you like, but I find it tends to pay to be precise here.