From: David Vallner Date: 2006-02-21T11:35:38+09:00 Subject: Re: Class variables and Constants Dňa Pondelok 20 Február 2006 14:50 dblack@wobblini.net napísal: > > AAAAAAAHHHHHHHHH!!!!!!!!!!!! > > I've been playing with this the whole night. I am not gonna stop until > > I've finished writing a clear document that explains absolutely > > everything. > I tried to do Poor Man's Traits in Java using static fields. I feel your pain. It's especially likely to cause initialization timing bugs - when I loaded the page from the webapp after a server restart, everything worked fine - by some strange coinkydink, the classes loaded earlier weren't used after they got clobbered by the subclasses loaded earlier. Lossage ensued after a page reload. > I think Matz is planning to change class variables in 2.0 so that they > are class/module scoped rather than hierarchy scoped. Meanwhile, > here's another fun variant: > > class A > end > > class B < A > @@var = 10 > def self.var > @@var > end > end > > class A > @@var = 20 > def self.var > @@var > end > end > > p A.var > p B.var > Oh dear. I do hope the change gets into 2.0, I can't wait for Java trolls to ramble against it... (Now don't tell anyone, but I have a hunch class instance variables actually work like you'd expect in this case. I think.) David Vallner