
Objects are a poor man's closures (2003) - j_baker
http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html
======
basman
One problem I've noticed with a very closure-heavy style is that debugging is
a pain — you have a closure with a bunch of local state and no way to print or
otherwise access it (from the repl, say). So then you end up writing special
cases of the function for when the first argument is :debug... any better ways
to do this?

~~~
tumult
Don't mutate data structures from within a closure :)

~~~
tedunangst
Even if you're not mutating the state, you may be interested in knowing what
it is.

~~~
loup-vaillant
Just look at the definition of your closure. If you didn't mutate the state
since then, well, it hasn't changed. Some debuggers even allow you to "travel
back in time", so you can easily "debug" the definition of that closure.

------
AndrewDucker
I do like that the poster is open minded and educated enough to realise that
both are reflections of each other. I particularly like the koan at the end.

~~~
wingo
The poster is Guy Steele, who when implementing Scheme, found out that his
implementation of objects (actors) was actually the same as his implementation
of closures.

[Edit: the poster is actually Anton van Straaten, replying to Steele]

~~~
zitterbewegung
Also anton van straaten edited r6rs.

------
btilly
The title should be amended to point out that this is from 2003.

My summary of the difference has always been, "Objects make good nouns while
closures make good verbs."

~~~
barrkel
Unfortunately, in many situations you also need to ask noun-like questions
about your closures.

For things like loop bodies, conditionals passed to filters, comparisons
passed to sorts, etc., closures work very well. But when you start using a
closure to e.g. represent an I/O endpoint (e.g. () => string for reading,
string => () for writing to / from a console), you'll also want to do things
like flush buffers.

~~~
btilly
That's why closures are a poor man's objects. :-)

~~~
loup-vaillant
_When_ , not why :-)

------
asimjalis
One difference between objects and closures is that objects require explicitly
passing state into them, while closures can capture state from their context.
This is powerful because it lets you explore different abstractions cheaply
with closures. Programming in Lisp I frequently stumble upon abstractions that
I would not have noticed if I was programming in Java or some other closure-
less language. For example, a button is just a closure. It supports exactly
one operation: click.

~~~
j_baker
This can also be a bad thing. If you explicitly pass in parameters to
something, it's much easier to tell where certain pieces of data are coming
from.

~~~
asimjalis
Right. More explicit means less fluid. So there is a trade-off. Fluidity might
not be important when the design is well-known and fixed. But if you want to
use code as a medium for exploring design, then fluidity is powerful.

------
d0m
With macro and closures, you can implement your objects as you wish; so in
this way of thinking, you don't need to "build it again and again"... it's
just that depending of the problem, there are better ways. (i.e. sort_by vs
overriding operator< or building a functor someone?)

------
nopassrecover
Pencils are a poor man's keyboard. There are different sets of things you can
do with both. You might be able to draw on a screen with the arrow keys but
that doesn't mean you should.

------
eplanit
Here's one very content poor man, then.

~~~
j_baker
Sounds a bit like Maslow's hammer, does it not?

<http://en.wikipedia.org/wiki/Law_of_the_instrument>

------
JoelMcCracken
Apples are a poor man's oranges.

~~~
SlyShy
I don't think this analogy applies, insofar as you can't implement apples with
oranges. ;) However, anyone who has worked through SICP will tell you, you can
implement objects with closures.

~~~
_delirium
True, though as the article points out, you can also implement closures with
objects. Of course, you can also implement both in C. ;-)

~~~
stcredzero
In certain languages you can accomplish closures and objects without enough
rope to hang yourself in truly spectacular fashion.

Seems a reasonable personal preference when put that way.

------
gcb
as a computer scientist, he's a good car salesman.

~~~
gcb
heh. love how this comment keeps changing value over time. +4, -4, 0, -2....

~~~
pg
Actually it has never been positive since the first vote, let alone +4.

