From: Mark Hahn Date: 2001-12-29T09:00:56+09:00 Subject: [ruby-talk:29603] promoting self to subclass (was extending method of class A ...) Why not allow a method to "promote" it's object to a subclass of the object? I've wanted to do this before in ruby and Java. It should be somewhat easy in ruby and nearly impossible in a static language. How about making it: self.promote(Klass) or maybe Kernel::promote(Klass) ? The same code that does the promotion could set the extra instance variables. Or, even better, the initializer of the subclass could be called with params from the promote call: self = Klass.new(*params). In general this could be considered "assignment to self". Where I've run into this need is when an initializer realizes that, based on input params, there is a more specific class for this data. Another case is when an object is created and the "type" gets refined later. If you want a specific example of this being used, let me know. ----- Original Message ----- From: "Tomasz Wegrzanowski" To: "ruby-talk ML" Sent: Friday, December 28, 2001 3:12 PM Subject: [ruby-talk:29598] Re: extending method of class A to support arguments of class B by promoting `self' to class B > On Sat, Dec 29, 2001 at 07:59:42AM +0900, Mark Hahn wrote: > > > > > it should be promoted to Complex > > > > Is this possible? I've wanted to change an object's type before inside a > > method of that object, but I didn't think it was possible. > > > > Or did you mean "return" a Complex object. That is all you really need to > > do in the case you describe. > > I meant: temporary object of type Complex, initiated by Real object, should be created, > and method `*' of this temporary Complex object should be used instead of method `*' of Real object. >