Hi --

On Mon, 4 Jul 2005, Eric Mahurin wrote:

> --- Devin Mullins <twifkak / comcast.net> wrote:
>
>> Thanks for the example. I'm just wondering why
>>
>> class Foo
>>   def blah
>>     @@cnt += 1
>>   end
>> end
>>
>> couldn't have been a shortcut for [an optimization of]:
>>
>> class Foo
>>   class << self
>>     attr_accessor :cnt
>>   end
>>   def blah
>>     self.class.cnt += 1
>>   end
>> end
>
> I agree.  Class variables should just be a shortcut for
> instance variables of an object's class.  Having an independent
> and slightly different class variable concept seems redundant,
> confusing, and inconsistent.
>
> You could just say that these should be equivalent:
>
> @@x       # self.class.instance_eval{@x}
> @@x = y   # self.class.instance_eval{@x=y}

That's a breakage of encapsulation, though.  In its capacity as "just
an object", a class should have the same "rights" as any other object
-- specifically, the right to keep its instance variables to itself.

(I understand that no object's instance variables are safe from
instance_eval, but I still wouldn't want to see it hidden behind a
special syntax to the detriment of Class objects.)

I think I like Devin's idea better -- essentially:

   @@x      # self.class.x
   @@x = y  # self.class.x = y

though I would want the writing of the accessor methods to be explicit
(rather than having the @@x reference cause a singleton method to be
written on the class).  This perhaps favors the "a class is just an
object" state of things more than the "classes and their instances
should have a way to share variables" concept, and that in turn
probably reflects my own priorities.

I'm not a big fan of the @@x syntax at all, but to have it work more
smoothly with the whole instance variable system would be much better,
in my view, than having it be a whole separate system which looks
connected to it but isn't.


David

-- 
David A. Black
dblack / wobblini.net