Sunday, September 6, 2009

Classes = Structs plus Closures.

I was leading our Groovy book club this week and had an insight. We have Java programmers, C/C++ programmers and script writers so we try to use a wide range of examples to illustrate points.

We were discussing closures, which can be challenging for people unfamiliar with them. I was making that point that Closures are Objects which means they are instances of classes. Particularly, they are basically classes with a (generally) single method and no data members.

That made me think about Structs, which are basically classes without methods.
So, I had the thought that Classes = Structs plus Closures.

I'm not sure if this is a helpful insight yet, but anytime I can think of things in a new way I like to explore it a bit. I think the C/C++ programmers in the book club found it helpful, perhaps because they're already used to thinking about different types of objects such as structs. So, thinking about Closures as another kind of class was not such a stretch for them.

1 comment:

  1. see http://tunyurl.com/2m7p9:

    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 Scneme interpreter with a closure-based object system. He leaned 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.

    ReplyDelete