How high level is D?

Laurent Tréguier laurent.treguier.sink at gmail.com
Thu Nov 22 08:13:13 UTC 2018


On Thursday, 22 November 2018 at 00:14:40 UTC, NoMoreBugs wrote:
> (2) - its' completely unlike what a C++/Java/C# programmer 
> would expect (the 3 most widely used languages).
>
> If (2) weren't also fact, I would not feel inclined to mention 
> it.

You can leave Java out of this list though. The following Java 
code will compile and run:

------------------------------
public class Main
{
     private int _mainMember = 0;

     public static class InnerData
     {
         private int _innerDataMember = 1;
     }

     public static class InnerAccess
     {
         public void accessMembers(Main main, InnerData innerData)
         {
             System.out.println("Outer member from inner: " + 
main._mainMember);
             System.out.println("Other class member from same 
level: " + innerData._innerDataMember);
         }
     }

     public static void main(String[] args)
     {
         Main main = new Main();
         InnerData innerData = new InnerData();
         InnerAccess innerAccess = new InnerAccess();

         System.out.println("Inner member from outer: " + 
innerData._innerDataMember);
         innerAccess.accessMembers(main, innerData);
     }
}
------------------------------

It goes unnoticed since Java forces you to have one top-level 
class per module, but Java treats `private` exactly like D: at 
the module level. Within that module, any class can access any 
member from any other class, no matter if ti's marked as 
`private`.


More information about the Digitalmars-d mailing list