stevan / p5-mop-redux

A(nother) MOP for Perl 5
139 stars 36 forks source link

There is no way to set the superclass for a class once it's been created #153

Open tobyink opened 10 years ago

tobyink commented 10 years ago

It would be nice to be able to do:

mop::meta("Foo::Baz")->set_superclass("Foo::Bar");

Setting a class' superclass in normal runtime code is (naturally) insanity. But setting it within a class trait is vaguely sensible. For example:

class Foo::Baz is myplugin { }

Might want to set the superclass for Foo::Baz, or might add roles to it, before registering it in some plugin list.

tobyink commented 10 years ago

PS: I have tried the below. It doesn't work, but I have no idea why it does not.

        mop::meta(ref $class)
            ->get_attribute('$!superclass')
            ->store_data_in_slot_for($class, $parent);
        mop::apply_metaclass($class, mop::meta($parent));
doy commented 10 years ago

It doesn't work because you're setting the metaclass for mop::class in that snippet. The first line should just be $class, not mop::meta(ref $class).

tobyink commented 10 years ago

Super-d'oh!

tobyink commented 10 years ago

I take that back: I believe mop::meta(ref $class) is correct. This gets the $!superclass attribute for mop::class itself, and then calls store_data_in_slot_for on that attribute passing $class (i.e. the meta object for the class I'm wanting to set the superclass of) as the instance.