How high level is D?
Paulo Pinto
pjmlp at progtools.org
Fri Nov 23 07:26:28 UTC 2018
On Thursday, 22 November 2018 at 08:13:13 UTC, Laurent Tréguier
wrote:
> 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`.
Actually that is wrong, it is one PUBLIC class per module.
public class Main
{
private int _mainMember = 0;
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);
}
}
class InnerData
{
private int _innerDataMember = 1;
}
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);
}
}
Main.java:11: error: _innerDataMember has private access in
InnerData
System.out.println("Inner member from outer: " +
innerData._innerDataMember);
^
Main.java:25: error: _mainMember has private access in Main
System.out.println("Outer member from inner: " +
main._mainMember);
^
Main.java:26: error: _innerDataMember has private access in
InnerData
System.out.println("Other class member from same
level: " + innerData._innerDataMember);
More information about the Digitalmars-d
mailing list