[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Infinite loop with Singleton mixin

Yukihiro Matsumoto

2/25/2009 7:02:00 AM

Hi,

In message "Re: Infinite loop with Singleton mixin"
on Wed, 25 Feb 2009 13:45:41 +0900, Adam Gardner <adam.oddfellow@gmail.com> writes:

|This loops inifitely, but it doesn't seem like it should.

Uninitialized singleton object should not be disclosed, I think. So I
think it should loop infinitely, as you specified.

matz.

1 Answer

Adam Gardner

2/25/2009 5:37:00 PM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: Infinite loop with Singleton mixin"
> on Wed, 25 Feb 2009 13:45:41 +0900, Adam Gardner
> <adam.oddfellow@gmail.com> writes:
>
> |This loops inifitely, but it doesn't seem like it should.
>
> Uninitialized singleton object should not be disclosed, I think. So I
> think it should loop infinitely, as you specified.
>
> matz.

Personally, I disagree. I think both consistency and utility would be
better served by having .instance return the instance as soon as
initialize has started - after all, there are plenty of other ways to
get references to objects which haven't finished their initialize
function, and they don't cause undue trouble. But I can understand why
it might need to be the way it is. I'll try and work around it.

Incidentally, it turns out the code above isn't looping the way I
thought it was; it's actually stuck inside the while loop in
singleton.rb:148-152

while false.equal?(@__instance__)
Thread.critical = false
sleep(0.08) # timeout
Thread.critical = true
end

Which, itself, suggests a work-around:

----
require 'singleton'

class SingleThing
include Singleton
def initialize
self.class.instance_variable_set(:@__instance__,self)
@a = OtherThing.new
end
end

class OtherThing
def initialize
@single_ref = SingleThing.instance
end
end

a = SingleThing.instance
----

Anyway, thanks for your attention, and thank you for your hard work on
Ruby.

--
Posted via http://www.ruby-....