James Coglan
6/20/2008 8:49:00 AM
[Note: parts of this message were removed to make it a legal post.]
2008/6/20 Calamitas <calamitates@gmail.com>:
> On Thu, Jun 19, 2008 at 3:23 PM, James Coglan <jcoglan@googlemail.com>
> wrote:
> > So anyway, this question has been really bugging me: in Ruby, how come
> > metaclasses are just that: classes? Given that you cannot instantiate
> them,
> > is there any reason why they need to be classes instead of just modules?
> I
> > want this release to be as close to Ruby as possible so if I've seriously
> > misunderstood something I'd rather be put right.
>
> Well, I don't think instantiability by itself is a requirement for a
> class. Binding is a class, but you can't instantiate it (yourself)
> either. Not quite the same, I know. The singleton pattern prohibits
> explicit instantiation. Again, slightly different maybe, although very
> much the same as in both cases there are somewhat artificial
> restrictions built in to enforce a concept.
>
> But do singleton modules make sense? Modules can be included in other
> classes, but it makes little sense to include a singleton module in
> another class because then it would no longer be a *singleton module*.
> So singleton modules would be unincludable modules.
>
> So what's better, uninstantiable classes are unincludable modules?
> Somewhat arbitrary choice, but as mentioned above, uninstantiable
> classes have precedent.
Singleton classes cannot be 'included' either, which is to say they cannot
be subclassed.
irb(main):006:0> class SillyArray < Array
irb(main):007:1> end
=> nil
irb(main):009:0> s = SillyArray.new [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):011:0> s.class
=> SillyArray
irb(main):012:0> s.class.superclass
=> Array
irb(main):013:0> class MetaArray < [].metaclass
irb(main):014:1> end
TypeError: can't make subclass of virtual class
from (irb):13
from :0
What I'm trying to get at is: given that you can't really do anything
'classy' with a metaclass, it seems they could quite easily be modules
instead, and this makes more sense to me as they are just objects that store
methods. Why do they need to be this specific type of module (i.e. Class)?