in reply to May Thy Closures Be Blessed
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: May Thy Closures Be Blessed
by adrianh (Chancellor) on Apr 26, 2004 at 15:34 UTC | |
An interesting discussion would be the differences between this and Inside-out classes. Both are based on lexical scoping to enforce encapsulation. What are the benefits/drawbacks of either method? Off the top of my head: | [reply] |
by Abigail-II (Bishop) on Apr 26, 2004 at 16:35 UTC | |
Abigail | [reply] [d/l] |
by eserte (Deacon) on Apr 26, 2004 at 17:43 UTC | |
| [reply] |
by adrianh (Chancellor) on Apr 26, 2004 at 21:23 UTC | |
by Abigail-II (Bishop) on Apr 26, 2004 at 21:51 UTC | |
| |
|
Re: May Thy Closures Be Blessed
by Abigail-II (Bishop) on Apr 26, 2004 at 16:46 UTC | |
Both are based on lexical scoping to enforce encapsulation.But an entire different kind of encapsulation! The closure based method as outline does data inheritance. By default, and you'll need to do work to prevent it. Inside Out objects don't do data inheritance (except if you define your subclass in the same lexical scope as your superclass - but you probably have good reasons to do so). Furthermore, Inside Out objects don't require their superclass, or subclasses, to cooperate. With the Inside Out technique, you can subclass anything, including a closure, without interfering. Even if the superclass changes its implementation, you're safe. The closure based strategy only works if the entire inheritance tree uses the same closure. Inside Objects are about maximizing freedom - it does not impose, and it does not require. ;-) Abigail | [reply] |
by hardburn (Abbot) on Apr 26, 2004 at 17:58 UTC | |
I wasn't at all surprised at the many comparisons to Inside-Out Objects that popped up in this thread. They both see basically the same thing wrong with Perl's object system as it is normally used and try to fix it in different ways. I must admit that the closure method didn't do some things as well as I thought it would when the idea first popped into my head a few days ago. With Inside-Out Objects, everything starts to look like a hash. With Closure Objects, everything starts to look like a subroutine. A subroutine call is ultimately more flexible than a hash lookup (unless you want to open the can-of-worms that is a tied interface). However, you have to put more work into it. IMHO, Inside-Out Objects rely on subtle behavior on Perl's part that make some people feel uneasy. Further, the technique isn't necessarily easy to grasp, even to those that have already mastered Perl's regular object system. Closures aren't necessarily easy, but I suspect they will be easier to think about than the Inside-Out technique. They may also appeal more to those with a functional programming background. Yes, Closure Objects do impose that the heirarchracy all use the closure technique. I think it's a net gain for freedom, even over Inside-Out Objects. It will ultimately depend on what you're trying to accomplish, though I suspect either technique will solve your problem if you put enough effort into it. Further, which technique is easier for your project may not be clear from the start. ---- Note: All code is untested, unless otherwise stated | [reply] [d/l] |
by Abigail-II (Bishop) on Apr 26, 2004 at 20:05 UTC | |
They both see basically the same thing wrong with Perl's object system as it is normally used and try to fix it in different waysWhile it may solve the problems you see with Perl's OO system, it doesn't solve any of the two biggest problems I see with the system: default data inheritance and no compile time checking of attribute names. Well, I guess one could say it solves the data inheritance problem - but in an extremely awkward way: the superclass defines the attributes of the subclasses. At least, that's how inheritance is happening in your example. Not what I call a real invitation for code reuse. With Inside-Out Objects, everything starts to look like a hash. With Closure Objects, everything starts to look like a subroutine. A subroutine call is ultimately more flexible than a hash lookup (unless you want to open the can-of-worms that is a tied interface). However, you have to put more work into it.This sound like false advertisement to me. First, your closure method eventually stores the attributes in a hash. You are just using the closure to give each object its private lexical hash. The closure doesn't provide any additional flexibility - your methods are already doing that. Methods provide the necessary flexibility, regardless of the way of storing attributes - directly in the object, in lexical class level hashes (for Inside Out objects), or in lexical object level hashes (for your closure technique). IMHO, Inside-Out Objects rely on subtle behavior on Perl's part that make some people feel uneasy. Further, the technique isn't necessarily easy to grasp, even to those that have already mastered Perl's regular object system.However, anyone having studied Damian's OO book will notice that Inside Out objects are a simple extension on "fly-weight" objects. They may also appeal more to those with a functional programming background.Frankly, I fail to see how they connect to 'functional programming'. Anyway, for a totally different way of doing OO using closures (no 'bless', but with inheritance, AUTOLOADING and SUPER), look at one of my earlier attempts to bypass Perl's OO problems: Read more... (11 kB) | [reply] [d/l] |
by hardburn (Abbot) on Apr 26, 2004 at 20:22 UTC | |
by Abigail-II (Bishop) on Apr 26, 2004 at 21:24 UTC | |
by tilly (Archbishop) on Apr 27, 2004 at 01:39 UTC | |
| [reply] |
by adrianh (Chancellor) on Apr 26, 2004 at 17:08 UTC | |
Furthermore, Inside Out objects don't require their superclass, or subclasses, to cooperate. They do need to co-operate over DESTROY. | [reply] |
by Abigail-II (Bishop) on Apr 26, 2004 at 19:33 UTC | |
They do need to co-operate over DESTROY.Yes, but not more than any other form of inheritance does. If you subclass a class, and define a DESTROY method in your subclass, without calling DESTROY in the superclass, things are likely to break. Your DESTROY method should always contain something like (for single inheritance): regardless whether you use Inside-Out objects or not. Abigail | [reply] [d/l] |
by adrianh (Chancellor) on Apr 26, 2004 at 21:58 UTC | |