closures and objects [closed]

2019-01-22 15:01发布

Functional programming .. is like classic ( Mark Twain's type). While reading another articles about SICP, where people are talking about the great impact closures had on there thinking, i got reminded of this, which i read ages ago

Closures are poor man's objects Objects are poor man's closure

( Can't recall exact source but it was programmers rosetta stone, or zen of programming, or tau of programming ... google has become so crowded that can't go to original source )

So co-programmers ... What is your take ... are closure something that you were always missing .. or just some syntactic sugar, which a preprocessor can do !!

5条回答
Anthone
2楼-- · 2019-01-22 15:36

Closures and anonymous blocks are pretty important programming constructs and I hit the lack fast in java, C++, C, and about any other language that's missing them. They are especially useful with asynchronous and eventing stuff. They also make function-taking functions more enjoyable to use and provide choices to do more things without additional syntax. (see: smalltalk and scheme)

查看更多
戒情不戒烟
3楼-- · 2019-01-22 15:37

I've been using closures for a long time in Javascript. Sometimes they would form an 'object', other times they're a useful way to provide instance data to a callback.

As far as I'm concerned they're another useful tool in the box but it goes beyond just sugar. Sugar relates to something you could reasonable do with another somewhat more convoluted syntax. Acheiving closures in a language that doesn't directly support them would be very difficult.

查看更多
趁早两清
4楼-- · 2019-01-22 16:00

http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html

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.

-- Anton van Straaten

查看更多
虎瘦雄心在
5楼-- · 2019-01-22 16:00

Closures are much more than syntactic sugar although I think it depends somewhat on your definition of "syntactic sugar".

To me, Java 1.5's for-each was syntactic sugar, but, for example, Ruby blocks go far beyond that. Closures provide a convenient level of abstraction, which is useful for implicit declaration of intent -- a big step toward DSL-ish syntax.

查看更多
成全新的幸福
6楼-- · 2019-01-22 16:00

closures are like objects in OO languages. But functional languages lack subtyping and inheritance.

查看更多
登录 后发表回答