From: Jonathan Worthington <[EMAIL PROTECTED]> Date: Thu, 21 Jun 2007 00:05:00 +0100
Hi, 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? . . . How about writing the method without the :method modifier, but also addmethod'ing it to the class? Then it will be found by find_method, but won't think the first parameter is an invocant. Something here doesn't feel quite right to me, though I'm tired and can't put my finger on what. Jonathan But in order to be able to use class attributes, you would want the invocant to be the class metaclass instance, wouldn't you? If so, then I think this should be handled by a mechanism for defining a method on a particular object, which is useful for more than just metaclasses. I know of only two ways to define a method particular to an instance X (though this may say something about my lack of breadth in the language department): 1. In Ruby, the class of X is transparently subclassed into a "virtual" class that becomes the class of X only, and the method is defined (and dispatched) normally on the virtual class. 2. In Common Lisp, on can define a method for the object X, and this is handled by the dispatch machinery. Since the consequences of these strategies are visible to the programmer ("foo.class" in Ruby returns something different after "def foo.meth . . .", but the class remains unchanged in Lisp), it seems like our hands are tied; both must be implemented in order to support both languages. But it's not clear to me that either needs to be in the core, nor which one would make the better default. Does anyone know of other strategies? For other languages with metaobjects (and, again, I confess I am not familiar with any), how do they do it? -- Bob Rogers http://rgrjr.dyndns.org/