Private visible?

xs0 xs0 at xs0.com
Fri Jul 14 07:32:19 PDT 2006


>> class Number {
>>     public void setVal(int val) { ... }
>>     public void setVal(long val) { ... }
>> }
>>
>> class IntNumber : Number {
>>     public void setVal(int val) { ... }
>>     private void setVal(long val) { assert(0); }
>> }
>>
>> Number a = new IntNumber();
>> a.setVal(10L);
> 
> With 162, it _does_ fail to compile if you move the class definitions to 
> another module. If the code is all in the same module, then the assert 
> trips. Both of these seem to work Ok to me.

Are you sure? I just checked again and the assert doesn't trip, nor does 
it fail to compile. Here is the exact code I used:

module main;

import ba;

void main()
{
	Number a = new IntNumber();
	a.setVal(10L);
}
==================
module ba;

class Number {
     public void setVal(int val) { }
     public void setVal(long val) { }
}

class IntNumber : Number {
     public void setVal(int val) { }
     private void setVal(long val) { assert(0); }
}


>> Reducing visibility should be forbidden, as it doesn't even work and 
>> leads to bugs.
>>
>> Private members should be totally invisible, because that's the point 
>> of marking them private.
> 
> IMHO, that may be a little strong...
> 
> IIRC 'protection' is universally termed "access protection" - not just 
> in D but when describing OOP protection generically or specifically for 
> almost any other OOP language. In other words, it specifies 'access' not 
> 'visibility'.

Well, I've no idea what term is the exactly right one to use, but I 
wanted to say that outside their defining module, private stuff should 
have no effects at all on any other code, as if it wasn't there at all.


xs0



More information about the Digitalmars-d mailing list