Exposing the Garbage Collector

2005-07-23 Thread Piers Cawley
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 :default10 }';

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

One way to do this is for a class to keep track of all its instances, which is
all very well until you start subclassing because subclasses have to both keep
track of their instances and inform their superclasses about any new instances,
which seems an awfully heavyweight thing to have to do just to be ready for the
occasional programmer like me who wants to develop classes in an image based
IDE or someone else who wants to add a role to a class at runtime or
whatever. Also, the class's links to its instances needs to be week, otherwise
you end up with instances that never get collected, which isn't good. 

It seems to me, that the way to get at all the instances of a class is to ask
the Garbage Collector to do the heavy lifting for us, and ideally I'd like to
see this exposed at the Perl level. It doesn't really have to expose that many
methods of course -- a simple map/grep would do nicely, then it'd be possible
to write, say:

class Class {
  method all_instances ($class:) {
$*GC.grep {$^obj.isa($class)}
  }
}

Note that, *of course* this is slow, but I suggest that this sort of thing
should be slow (or, at the very least, you shouldn't require everything else to
pay a runtime cost just to allow people to use reflective tools like this).



Re: Exposing the Garbage Collector

2005-07-23 Thread Brent 'Dax' Royal-Gordon
Piers Cawley [EMAIL PROTECTED] wrote:
 It seems to me, that the way to get at all the instances of a class is to ask
 the Garbage Collector to do the heavy lifting for us, and ideally I'd like to
 see this exposed at the Perl level.

It's entirely possible that Perl will be used on virtual machines
where this can't be done.

I'd suggest that we simply declare that every metaclass must support a
.all method which returns an arrayish thing containing all active
objects it is managing:

   grep { .isa($class) } MetaClass.all;

Whether this works by interfacing with the garbage collector, keeping
an array of weak references, or waving a wooden wand and yelling
Accio objects is completely up to the metaclass in question.

(Sorry, been reading too much Potter lately...)

-- 
Brent 'Dax' Royal-Gordon [EMAIL PROTECTED]
Perl and Parrot hacker


Re: Exposing the Garbage Collector

2005-07-23 Thread chromatic
On Sat, 2005-07-23 at 20:41 -0700, Brent 'Dax' Royal-Gordon wrote:

 Piers Cawley [EMAIL PROTECTED] wrote:

  It seems to me, that the way to get at all the instances of a class is to 
  ask
  the Garbage Collector to do the heavy lifting for us, and ideally I'd like 
  to
  see this exposed at the Perl level.
 
 It's entirely possible that Perl will be used on virtual machines
 where this can't be done.

Will those VMs support string eval?  If not, Piers' *specific* problem
here mostly goes away.  How much introspection and intromanipulation
should Parrot guarantee on small or limited platforms in general?

-- c