On Wed, Jun 20, 2007 at 10:38:08PM -0400, Bob Rogers wrote: > From: "Patrick R. Michaud" <[EMAIL PROTECTED]> > On Wed, Jun 20, 2007 at 09:41:00PM -0400, Bob Rogers wrote: > > Patrick R. Michaud wrote: > > > My first question is "How do I add a class method?" -- i.e., > > > a method that operates on a class instance as opposed > > > to an object instance of that class . . . > > > > A method that operates on a class instance would be an instance method > > of the class metaclass, wouldn't it? But I think you meant "on a > > *particular* class [metaclass] instance", am I right? > > Yes, I believe this is a better phrasing. If I want to define a > new method or or override an existing method on a metaclass instance > [a class], then how do I do that? > > AFAICS, there isn't currently a good way to do that. (Depending, of > course, on whether you think Jonathan's suggestion qualifies. ;-) > Nor does PDD15 seem to cover this; it actually says very little about > metaclasses, beyond just the fact that they exist (and even then only > mentions class metaclasses).
To somewhat see where I came from in this, pdd15 has a =head3 section titled "Class Methods", where all of the methods appear to be operations on classes, as opposed to instances of the class. There's also a line in pdd15 that reads: =item - Classes are instances of a meta-class and have their own sets of class methods and class attributes So, I was wondering how one would create a class method. > The particular instance I'm looking at is a C<Str> class for > Perl 6, where I want the get_string vtable function for the > C<Str> metaclass instance is different from get_string of > a C<Str> class object. > > To me, the phrases "C<Str> metaclass instance" and "C<Str> class object" > sound like the same thing. Did you mean "C<Str> object" in the second > case? I did. Apologies for the ambiguity. > If so, then get_string on the class metaclass would just affect > the way the class stringifies, and not any of the instances, but I don't > get the point of that. So I'm sure I must be misunderstanding you. Do > you have an example? Maybe the perl6 code you mention? Well, after re-reading S12 this evening I've determined that the perl6 example I was thinking of isn't precisely correct for the current spec, and I think it's better not to confuse the issue (or myself) by trying to describe my incorrect example. (Essentially, it had to do with the way the Str class stringifies as opposed to the way a Str object stringifies.) But, I think I have another way to describe what I'm asking. In an earlier message [1] Allison wrote that the 'typeof' operator will now return different values for class objects and instances of the class. For example: $P0 = newclass "Foo" $P1 = new $P0 $S0 = typeof $P0 # returns "Class" $S1 = typeof $P1 # returns "Foo" I was wanting to know how to do something similar, except with get_string instead of typeof. In particular, the example I've been thinking of is: $P0 = get_class "Str" # get the Str class $P1 = new $P0 # create a Str instance assign $P1, "XYZ" # give it a value $S0 = $P0 # returns "Str" $S1 = $P1 # returns "XYZ" In short, I was wondering how I would get a class to stringify to its name (or possibly some mocked value), while having the instances of that class continue to stringify to their values. As I mentioned above, my re-reading of S12 may make this particular question moot, although it's important to note that Jonathan apparently encountered something like it as well. :-) At any rate, I hope the example here hopes to clear up what I was looking for. And thanks for being so patient as I try to get an accurate description together. :-) Pm [1] http://www.nntp.perl.org/group/perl.perl6.internals/2007/06/msg38608.html