On 2/8/06, Jonathan Lang <[EMAIL PROTECTED]> wrote:
> Stevan Little wrote:
> > Yes, that is correct, because:
> >
> > Dog.isa(Dog)  # true
> > $spot.isa(Dog)  # true
> > ^Dog.isa(Dog)  # false
> >
> > In fact ^Dog isa MetaClass (or Class whatever you want to call it).
> >
> > At least that is how I see/understand it.
>
> OK.  To help me get a better idea about what's going on here, what
> sorts of attributes and methods would ^Dog have?

Well, a metaclass describes the behaviors and attributes of a class,
and ^Dog is an *instance* of the metaclass. So actually ^Dog would not
actually have attributes and methods since it is an instance. That
said, I think ^Dog would probably respond to methods like these (some
of which are described in S12):

  ^Dog.name # Dog
  ^Dog.version # 0.0.1 (or something similiar of course)
  ^Dog.authority  # cpan:LWALL or email:[EMAIL PROTECTED]

  ^Dog.identifier # returns the string Dog-0.0.1-cpan:LWALL

I would like to see some methods like this:

  # dynamically add a method that
  # Dog and $spot would respond to
  ^Dog.add_method(bark => method () { ... });

Which would be like doing this in Perl 5:

  no strict 'refs';
  *{'Dog::bark'} = sub { ... };

And of course if you can add a method, you will need to be able to
fetch and delete them as well, so a &get_method and &remove_method
would be in order as well.

And if you can add methods, surely you can add attributes, so
&(add|get|remove)_attribute would be needed.

  ^Dog.add_attribute(:label<$fur>, :access<rw>);

Would be equivalent to saying this:

  class Dog is reopened {
      has $fur is rw;
  }

And ^Dog would also provide access to infromation about super and
subclasses as well. So &superclasses and &subclasses methods would
make sense too. We would also need methods to deal with Role
relationships as well.

So, given the above items, the class MetaClass might look something like this:

  class MetaClass {
      has $name is rw;
      has $version is rw;
      has $authority is rw;

      has @superclasses;
      has @subclasses;

      has %methods;
      has %attributes;

      method identifier { ... }

      method superclasses { ... }
      method subclasses { ... }

      method add_method { ... }
      method get_method { ... }
      method remove_method { ... }

      method add_attribute { ... }
      method get_attribute { ... }
      method remove_attribute { ... }
  }

So given this, you could almost look at this code:

  class Foo-0.0.1-cpan:JRANDOM {
      has $bar is rw;

      method baz (Foo $self:) { ... }
  }

As being roughly equivalent to the following code:

  ^Foo := MetaClass.new();
  ^Foo.name('Foo');
  ^Foo.version(0.0.1);
  ^Foo.authority(:cpan<JRANDOM>);

  ^Foo.add_attribute(:label<$bar>, :access<rw>);
  ^Foo.add_method(baz => method (Foo $self) { ... });

Of course this is mostly unspecced, and it is still unclear exactly
how much of this meta-level API will be accessible in Perl 6 itself.
And as far the the Pugs work on this goes, we plan to have something
similar to the above available in the next release (6.28.0).

Hope this helps.

Stevan

Reply via email to