Module-level visibility

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 16 07:36:22 PST 2010


On Tue, 16 Feb 2010 10:23:17 -0500, retard <re at tard.com.invalid> wrote:

> Mon, 15 Feb 2010 15:48:49 -0800, Ali Çehreli wrote:
>
>> bearophile wrote:
>>> I have shown this little program to some of my friends that program in
>>> Java/C#:
>>>
>>> class Foo {
>>>     private int x;
>>> }
>>> void main() {
>>>     Foo f = new Foo;
>>>     f.x = 5;
>>> }
>>>
>>>
>>> When I say them this code compiles with D2 they usually tell me that
>>> the compiler has a bug. So I suggest Walter to ask other people, maybe
>>> at Google or else (and in this newsgroup too), if they think this
>>> feature of D2 is a good thing, before this feature is set in stone in
>>> D2 (I have no definite answer about this topic, I can't help you). If
>>> you are really sure this is a good feature, then you can ignore this
>>> post.
>>>
>>> Bye,
>>> bearophile
>>
>> I don't have much experience with D, but I think this feature is useful.
>> It solves the problem of giving access rights to a select few, in
>> addition to the class itself.
>>
>> In C++, it is sometimes sought to allow some classes to have more rights
>> than the general public. For example, let the public have read access to
>> certain features, but let some classes have write access.
>>
>> In D, that select few is the module by default, and may be extended to
>> the package.
>
> In languages like Java/C#/Scala, there is no friend attribute. Still
> major applications can be built. According to some OOP principles
> accessing the internal state is often a design error. There might be some
> efficiency issues, but other than that, I guess the large number of Java/
> C# programs clearly shows that 'friendship' is not a critical feature.
> IMHO the implicit friendship feature inside the same module causes more
> problems than advantages also in D.

Sure they do.  It's called package.

What D doesn't have is essentially Java's private protection -- protect  
this data, even from classes in the same package (module).  In the end, I  
think it's a wash, since the person writing a module is writing all the  
classes and knows what each class does.  Is it nice to be able to say,  
even within this module, disallow access?  Perhaps.  But perhaps it's not  
so important.  Experience is on the side of Java, but it's difficult to  
say that D's approach is wrong.

It threw me off the first time I saw it too, but I just don't think about  
it now.  It generally doesn't get in the way.

I don't really have any problems with friend in C++, I just look at D's  
method as another way to do it without using a keyword.

-Steve



More information about the Digitalmars-d mailing list