From: dblack@... Date: 2007-09-02T05:42:06+09:00 Subject: Re: Singleton Modules rather than Singleton Classes Hi -- On Sun, 2 Sep 2007, Logan Capaldo wrote: > On 9/1/07, Trans wrote: >> This recently came up in the thread entitled "Python-style >> Decorators", so I thought it a fair idea to "formally" put it before >> the Ruby community. Here's the deal... >> >> Singleton classes already have more in common with modules than >> classes by the very behaviors that distinguish a module from a class -- >> they cannot be instantiated and they can not be inherited. So why >> exactly do we deem them classes at all? If instead we took them to be >> actual modules, and used as such, it would open up some really nice >> possibilities. For example: >> > > Counter proposal: remove singleton classes all together in favor of > simply having singleton methods. The useful facility of a singleton > class is that it allows you to have per-object methods (singleton > methods). Class and module are both misnomers for the "thing that acts > as a place to store singleton methods." By eliminating the visibility > of this implementation detail, it allows for other implementations to > be experimented with. The more you pin down how singleton methods are > implemented, and the more you start doing things with details of that > implementation the hard it becomes to have flexibility in that > implementation. This also removes the need to justify referring to > them as classes, and the false expectations this creates. My 2cents.. But then you introduce a whole second model of how method lookup and so forth works. What's nice about singleton classes is that they fit into the same basic model as other classes; once the premise is granted that every object can have a singleton class as well as a "birth" class, it all flows from there. I like the fact that every method lives in a class or module. I would actually be happy for them to be singleton modules instead of classes, though I don't really like the idea that one object can extend itself with another object's singleton methods. Or, to put it another way, I do like the possibility of strictly per-object behavior, so I wouldn't want to see that done away with. Maybe if it were done explicitly via dup'ing of some kind it would be OK. Otherwise it's just multiple objects sharing a module, which is basically what the non-singleton scenario already is. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)