Piers Cawley wrote:
Let's say I have a class, call it Foo which has a bunch of attributes, and I've
created a few of them. Then, at runtime I do:
   eval 'class Foo { has $.a_new_attribute is :default<10> }';
Assuming I've got the syntax right for defaulting an attribute, and lets assume
I have, the Perl runtime needs to chase down every instance of Foo or its
subclasses and add an attribute slot (adding a method to a class is nowhere
near as interesting, because every instance of the class shares a single class
object).

But adding an attribute is simply defining a new method in the Class' instance methods. And you don't need to chase down the objects for that.

What you wrote could be implemented as;

  class Foo {
     has $a_new_attribute is :default(10);
     sub a_new_attribute is accessor
        :FETCH{ $a_new_attribute },
        :STORE -> $val { $a_new_attribute = $val },
     }
  }

So this way the extra attribute can be lazily added. The other ways of finding out that the property should be there, such as the visitor pattern interface, will "trip" the part that adds the slot to the object.

However I think this is somewhat besides your original point.

Sam.

Reply via email to