From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2001-08-12T16:24:13+09:00 Subject: [ruby-talk:19580] Re: Why not access control for instance and class variables? In article <20010812.150024.102578912.gotoken@notwork.org>, GOTO Kentaro wrote: >From: ptkwt@shell1.aracnet.com (Phil Tomson) >Subject: [ruby-talk:19574] Why not access control for instance and class >variables? >Date: Sun, 12 Aug 2001 14:04:08 +0900 >> Maybe this is a silly question, but with all the recent talk about how >> subclasses can modify class variables >> and how that is possibly dangerous... why can't the access control >> functions (public,protected,private) be applied to class variables and >> instance variables as well? So that if you had: > >Recently, some possibilities were discussed at Japanese list. The >goal is to avoid unexpected name confliction. > > (0) All @variable should be private (my opinion, but this has been > rejected because of backward comptibility). Probably not a good idea at this point. > > (1) Gentlemen's agreement; a kind of special name should be given to > instance variables which is intended to be private. > > (2) Mangling feature something like Python's way may help the above. > In this case we would adopt new prefix, e.g., @_, for private. > For the sake of consitency, I would prefer if we could use the public,private,protected functions that are used for methods (also, if we don't watch out we'll end up running out of special characters like Perl :-) Now, to muddy the waters a bit, what about adding another dimension to all this access control for variables? What I mean is that there could be something like: private (:class_variable_name, 'r') Which would mean the the class variable @@class_variable_name would be readable by child classes but not writable (of course, there are probably better ways to do this but the idea I'm trying to get across is that it might be nice to have some kind of mechanism to allow class variables to be accessed by child classes, but not to be modified by them). Actually, as I think about it, private should mean that the class variable (or even instance variable) is not accessable at all to child classes, so perhaps the protected function would allow you to add read/write specifiers, but private would not, so it would look like: protected (:variable_name,'r') But then, it breaks the way protected currently works... maybe there is a cleaner way of doing this.... >By the way, can't private class variables be mimicked by constats? > Yes, I believe there were some code snippets posted on the other thread that dealt with the class variable issue. Phil