From: Jeremy Tregunna Date: 2005-04-02T17:15:04+09:00 Subject: Re: how to simulate self-style delegation [SuperHash] itsme213 wrote: > "Robert Klemme" wrote > > >>"Does it make sense to steal a method from a class and make it execute on >>another classes instance?" I mean, you cannot generally expect that all >>methods the "stolen" method requires are present in the other class. (I >>know that's why you use the fallback but then again, what does one gain? >>You can, for exaple, inject methods temporarily into the other instance.) > > > I think this is how delegation is used in object-based languages like SELF. > It's a way for B to dynamically inherit at the object-level from A, both > behavior and state. Copying behavior would need copying state, which may not > be practical (how deep to copy, what about state change, etc.). I'm not 100% sure about self, but some languages that are inspired from self seem to have similar behavior in this department, wherein you aren't actually copying anything; its all done via a prototype lookup chain. Which means, for example, that slot "foo" is not found in object "B", but it exists in object "A" which "B" was cloned from, "B foo" would be a valid message send because it would find the method as part of the lookup chain, "foo" is not copied from "A" to "B" (though you could if you really wanted too, but why bother? this is something that's done for free in a similar manner). (Just to clarify.) > Don't know if I explained it well. I'm sure I can find other approaches too, > like passing an explicit and optional _delegator_ parameter to all calls. I > was curious how well I could emulate SELF-like delegation as it would fit > very well. In Ruby? Not very easily in any way I can think of without doing some butchering, and dirty hacks (this isn't to say doing what you want isn't possible, I'm just not entirely sure, so I'll refrain from commenting further). -- Jeremy Tregunna