p5-mop-redux icon indicating copy to clipboard operation
p5-mop-redux copied to clipboard

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

Open tobyink opened this issue 11 years ago • 4 comments

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 avatar Nov 19 '13 15:11 tobyink

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));

tobyink avatar Nov 19 '13 15:11 tobyink

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).

doy avatar Nov 19 '13 16:11 doy

Super-d'oh!

tobyink avatar Nov 19 '13 16:11 tobyink

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.

tobyink avatar Nov 19 '13 16:11 tobyink