News for hackers. Stuff that matters.
Re:Ask HN: How do you explain closures? by shadytrees on Friday November 06, 2009 @10:23AM (#926105)
An interesting demonstration of why closures are useful is to start from a hypothetical language that supports closures but not objects and then implement a (simple!) object/class system. For me, it was extremely clarifying to have something in my mind click and go "Oh, OOP doesn't need to be a language feature ... there's this much more powerful concept available as a building block."
Re:Ask HN: How do you explain closures? by slmbrhrt on Friday November 06, 2009 @10:36AM (#926140)
The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."

Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.

On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.

Re:Ask HN: How do you explain closures? by jamesbritt on Friday November 06, 2009 @01:37PM (#926597)
Re:Ask HN: How do you explain closures? by jriddycuz on Friday November 06, 2009 @11:47AM (#926329)
You have much to teach us, great one.
Re:Ask HN: How do you explain closures? by gcv on Friday November 06, 2009 @11:18AM (#926242)
Yes. This approach works. I used it when I actually did work in a proprietary language which had no usable objects, but did have half-decent lambdas. Most people did not understand or use them at all, but one new guy with a Java background asked me how to cope. I explained to him how to use closures to implement a simple bank account class, and he got it immediately.

I don't think this guy played WoW, though. :)

Re:Ask HN: How do you explain closures? by tamas on Friday November 06, 2009 @10:41AM (#926156)
The idea is really nice, but I'm not sure it would work on the OP's Joe, who probably wouldn't even care about the hypothetical language and would regard the whole concept hypothetical, only for those who have way too much free time. (With WoW on the table, you don't have that luxury.)

I would take the route of first order functions -> anonymous functions -> closures, but I think it is really a lost cause for the typical 9-5 programmers.

Re:Ask HN: How do you explain closures? by gruseom on Friday November 06, 2009 @10:56AM (#926192)
I would take the route of first order functions -> anonymous functions -> closures

But those are three completely different concepts. A closure need not be anonymous or even first-order.

Re:Ask HN: How do you explain closures? by wingo on Friday November 06, 2009 @02:43PM (#926754)
You still want applicable objects, though, and closures don't quite cut it. (I say this as a schemer.)

See http://www.r6rs.org/formal-comments/comment-6.txt, for example.