From: Florian Gross Date: 2004-06-26T03:03:05+09:00 Subject: Re: Is it considered Harmful? Jim Weirich wrote: > The "standard" application for the self.class= method (aka become(class)) > is use it to do demand on load style proxies. Suppose you have a largish > object in external storage (database, file, whatever). You create a > small, efficient proxy item for the large object until you really need the > true data in the object. Then you load the object and let the proxy > "become" the newly loaded object. > > Without class=/become methods, > (a) the proxy remains and forwards all messages to the real object, or > (b) you replace every reference in the program to the proxy with > a reference to the real object. > > (a) is a small, but anoying constant cost, and (b) may be difficult, > depending on how wide spread the proxy was used. There's another small, but annoying problem with the proxy-approach: There are operations which can't be forwarded. I can only think of the truth value of an Object right now. It's a problem in this case: irb(main):001:0> [obj = false, ref = WeakRef.new(obj)] => [false, false] # We should now be able to treat obj and ref as # if they were actually the same thing irb(main):002:0> [obj.to_s, ref.to_s] => ["false", "false"] # This is okay, methods get forwarded irb(main):003:0> [if not obj then "foo" end, if not ref then "foo" end] => ["foo", nil] # truth value decisions can't be forwarded irb(main):004:0> exit I'm not sure how other languages handle -- as far as I know Perl has a way of overloading how Objects will be converted into Boolean values. Python also seems to have this via __nonzero__. (And __len__ == 0) I have no idea however if such a method (let's call it #to_bool) could be do in a way such that it doesn't disturb the performance of all Ruby scripts, but it looks more possible every day. Regards, Florian Gross