From: "Mauricio Fernández" Date: 2002-12-12T04:49:34+09:00 Subject: Re: all the pretty evals... On Thu, Dec 12, 2002 at 01:51:47AM +0900, Chris Pine wrote: [snip] > Yes, I can see that. :) > > I'm asking how this sort of singleton class differs from 'regular' > inheritance. What is the theory/rationale/model behind this? A singleton class is by definition associated to a single object. If you were given the possibility to instantiate singleton classes, that wouldn't hold any longer. class A end a = A.new klass = class << a; self; end b = klass.new # in non-existent Ruby def a.bar "bar" end b.bar # ? work cause the singleton is shared OR fail # cause another "singleton" was created In the second case, Ruby would have to stack "singleton classes", and you can see it'd quite hard to track. Up to one singleton class per method. GRR Singleton classes allow us to define methods on a per-object basis. This isn't compatible with making them "instantiable". > (If I asked why there was a law against killing people, I wouldn't feel my > question was answered if someone said, "Because, you see, killing is > illegal," and then referred me to the specific law. I'd be looking for > something more like, "Because killing is wrong, and because making killing > illegal makes it illegal for others to kill you.") > I think the reason for not being able to "inherit" specific properties of objects (since that's what instantiating singleton classes would mean) is that such things should be done on a class level. If some property is generic enough that somebody might want to use it, we should use a class of objects having that property, not take them from one specific object. You might find the following better: "because inheriting properties from single objects is wrong, and making 'singleton' classes really _singleton_ makes it illegal for you to try it" Then there are the matters of style and the YANGTNI thing, but I'm not matz so I cannot really use these points. BTW, something along the lines of ""instantiable" (not)singletons" would be irb(main):001:0> class A irb(main):002:1> end nil irb(main):003:0> B = A.clone A irb(main):004:0> a = A.new # irb(main):005:0> b = B.new # irb(main):006:0> class B irb(main):007:1> def bar irb(main):008:2> p "bar" irb(main):009:2> end irb(main):010:1> end nil irb(main):011:0> a.bar NameError: undefined method `bar' for # from (irb):11 irb(main):012:0> b.bar "bar" nil So you can add things to the cloned class without affecting other objects, but you can inherit from it if you want to. But of course what we really want then is 'class B < A' and not this nonsense. You can take it as a reduction to absurd proof of why '"instantiable" singletons' is meaningless :) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com If Bill Gates is the Devil then Linus Torvalds must be the Messiah. -- Unknown source