Sealed classes - would you want them in D?

KingJoffrey KingJoffrey at KingJoffrey.com
Sat May 12 06:35:55 UTC 2018


On Saturday, 12 May 2018 at 04:29:32 UTC, Neia Neutuladh wrote:
> On Friday, 11 May 2018 at 14:05:25 UTC, KingJoffrey wrote:
>> private is not private at all in D, and because of this, 
>> classes are fundamentally broken in D (by design apparently).
>
> I find this amusing because D does things exactly like Java. In 
> Java, two sibling nested classes can call private functions on 
> each other. But nobody says that this makes Java's classes 
> fundamentally broken.
>
> Like, this just works in Java:
>
> public class Protections {
>     public static class Bar {
>         private void bar() { System.out.println("bar"); }
>     }
>
>     public static class Baz {
>         private void bar(Bar b) { b.bar(); }
>     }
>
>     public static void main(String[] args) {
>         new Baz().bar(new Bar());
>     }
> }
>

Come on, your code example misses my point completely.

Take this program below, for example, and tell me that class 
encapsulation is not broken in D:

===============================
module test;

import std.stdio : writeln;

void main()
{
     Person p = new Person("King Joffrey");

     writeln(p.getName); // I designed my class to return this.
     writeln(p._name); // But D can bypass your intention 
completely.

     p._name = "New King"; // even worse, D can nominate another 
king.
     writeln(p._name);
}

class Person
{
     private string _name;

     public void setName(string name)
     {
         this._name = name;
     }

     public string getName()
     {
         return ProperName(this._name);
     }

     public this(string name)
     {
         _name = name;
     }

     private static string ProperName(string name)
     {
         return name ~ " : The one true king!";
     }
}

==============================


More information about the Digitalmars-d mailing list