5 ms·
Objects are just a poor mans closure. Closures are just a poor mans object. (Not sure where this quote is from originally? Maybe here: http://people.csail.mit
by goto11 6y ago
Objects are just a poor mans closure.
Closures are just a poor mans object.
(Not sure where this quote is from originally? Maybe here: http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m...)
- Someone 6y agoThere also is the classical example of defining cons, car, and cdr as closures in SICP: (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) Here, cons, instead of returning an object with two values, returns a closure that captures the two values. car and cdr call that closure, passing it another closure. The cons closure calls that closure, passing the the two values. Explanation at https://stackoverflow.com/questions/21769348/use-of-lambda-for-cons-car-cdr-definition-in-sicp https://stackoverflow.com/questions/21769348/use-of-lambda-f....
- chriswarbo 6y agohttps://en.wikipedia.org/wiki/Church_encoding https://en.wikipedia.org/wiki/Church_encoding
- Joker_vD 6y agoAlso, https://en.wikipedia.org/wiki/Scott_encoding https://en.wikipedia.org/wiki/Scott_encoding Of course, it completely kills (or "stress-tests", if you will) the branch predictor in your CPU.
- avmich 6y agoI like this explanation - https://stevelosh.com/blog/2013/03/list-out-of-lambda/ https://stevelosh.com/blog/2013/03/list-out-of-lambda/ .
- mikorym 6y agoClosures in which sense here? Like closure operators?
- munchbunny 6y agoThis closure https://en.wikipedia.org/wiki/Closure_(computer_programming) https://en.wikipedia.org/wiki/Closure_(computer_programming) Link because this describes it more thoroughly and more precisely than I can.
- goto11 6y agoClosure in the sense of a function which capture free variables in the outer scope. The captured variables are like fields in an object instance since they retain their state between invocations of the function. So a closure can be thought of like an object with a single method - hence a poor mans object :-)
- pietrovismara 6y agoAlso not necessarily a function, both rust and js support closures in brackets.
- masklinn 6y agoA block introduces a lexical sub-scope but it's not a closure, because there's no closing over: the "closing over" part is about a function accessing its definition context during its execution. A block doesn't have such a distinction.
- Sharlin 6y agoThis correspondence is made abundantly clear by C++ which explicitly defines its lambda functions ”as if” they desugared into objects of appropriate anonymous compiler-generated struct/class types. Also Java, which has had closures since day one, but used to call them ”anonymous inner classes” and forced people to use a ridiculously verbose syntax to define them.
- magicalhippo 6y ago
- kazinator 6y agoIt means that the "rich" man has objects and closures, for the situations where each one is the best fit.
- munificent 6y ago> Maybe here: http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m.. http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m.... As far as I know, yes, that's the oldest I've been able to find.
- btilly 6y agoSee https://www.perlmonks.org/?node_id=75831 https://www.perlmonks.org/?node_id=75831 for a similar sentiment from me a couple of years before that. My exact quote was, While closures may look like a poor man's object, I think the reverse is closer to the case.
- Animats 6y agoProbably the early schemes for doing objects in LISP, from the Symbolics refrigerator-sized LISP machine era, which worked that way.
- jtolmar 6y agoIt'd be nice to be able to blur the lines. Maybe implement multireturn by declaring variables public inside your closure scope. function Person(firstName, lastName) { let fName = firstName; let lName = lastName; public getFullName = function() { return fName + ' ' + lName; }; public setFirstName = function(firstName) { fName = firstName; }; return this; } ("return this" is probably not the best syntax at the end there, especially if bolting this onto javascript.)
- LudwigNagasena 6y agoI remember trying to think about something similar when I wanted to invent a language that tries to stay as close to “everything is an X” as possible. (But it was a mess, unfortunately) I would use “return scope”.
- jtolmar 6y ago"scope" was actually on my short list of possibilities :-)
- szatkus 6y agoThat's just Python classes.
- jschwartzi 6y agoThat's literally true in Lua, where there is no such thing as object-orientation but there's lots of ways to do object-oriented programming. The simplest, most performant way I've found is to return a hash map containing lexical closures and data from a "constructor" function. Any enclosed functions which refer to values declared in the constructor use those values in a closure, so it's easy to do instance or static member fields by putting the field declaration inside or outside of the constructor function. You can do it with metatable manipulation as well(changing the functions which map to specific table operators which lets you change how unmapped keys are handled) but I found the lexical closures to make a lot more sense.