From: "mame (Yusuke Endoh)" Date: 2012-04-20T04:53:11+09:00 Subject: [ruby-core:44471] [ruby-trunk - Feature #6308][Feedback] Eliminate delegation from WeakRef Issue #6308 has been updated by mame (Yusuke Endoh). Status changed from Open to Feedback Hello, 2012/4/17, headius (Charles Nutter) : > WeakRef's delegation features are a really awful pattern that should not be > allowed in future versions of Ruby. Maybe I understood your subject, but your proposal is not clear. Could you please make it concrete? "change the behavior of lib/weakref.rb in 2.0" => Impossible, because of matz's 2.0 compatibility policy. "change the behavior of lib/weakref.rb in 3.0" => Maybe possible if matz accepts. Please create a patch that warn a user when the delegation features are used. Then, I'll set this ticket as 3.0 issue. "deprecate lib/weakref.rb and add an alternative library" => Please show us the alternative first. -- Yusuke Endoh ---------------------------------------- Feature #6308: Eliminate delegation from WeakRef https://bugs.ruby-lang.org/issues/6308#change-26026 Author: headius (Charles Nutter) Status: Feedback Priority: Normal Assignee: Category: lib Target version: WeakRef's delegation features are a really awful pattern that should not be allowed in future versions of Ruby. WeakRef makes no guarantees as to the liveness of its contained object. It can be collected at any time if there are no strong references to it. WeakRef currently uses delegation to pass method calls through to the contained object. This encourages a pattern where a WeakRef is passed to methods that expect to have a reference to the underlying object, making it appear to be that object. Unfortunately, this is *never* a good idea. Because the object can be collected at any time, you may get a nil reference from __getobj__ *arbitrarily* in code that tries to call methods against the given WeakRef. That means using WeakRef as a delegate will always result in unreliable code, and errors may happen for inexplicable reasons. I believe Ruby 2.0 should eliminate WeakRef's delegation features and make it a simple reference holder. There's no safe way to use a weak reference except to grab a reference to the object, check that it is alive (non-nil) and then proceed with the use of the object, as follows: obj = weakref.__getobj__ raise AppropriateError unless obj obj.do_something obj.do_something_else Along with eliminating delegation, I would recommend simply making the get method #get, since the uglier #__getobj__ is only named that way because it is not delegated. -- http://bugs.ruby-lang.org/