Adding a new design constraint to D

forkit forkit at gmail.com
Sun Jun 26 00:05:44 UTC 2022


On Thursday, 23 June 2022 at 10:55:17 UTC, The Zealot wrote:
>
> ```
> module test;
> import std.stdio;
>
> @safe:
> interface IDerived {
>     void fine() @safe;
> }
>
> IDerived createDerived() {
>     static class Base { private int x = 100;  }
>     static class Derived : Base, IDerived { void 
> fine()@safe{writeln(x);} }
>     return new Derived();
> }
>
> void main()
> {
>     import std.stdio : writeln;
>
>     auto d = createDerived();
>     d.fine();
>     writeln(d.x); // look, no access.
> }
> ```

That is certainly interesting ;-)

However, it looks like a lot of work, just to limit access to a 
class member :-(

I was rethinking how I could 're-frame' my argument, in light of 
that 'Civility' thread.

Perhaps like this:

i.e. what is the value of having keyword 'const' in this code 
below?

I mean, if you want the string to remain constant, then don't 
change it.

Why bother using 'const'? Let's just get rid of const. It's kinda 
pointless, since you're the programmer, writing the code.

The answer is simple. The compiler can statically **guarantee** 
the correctness of your program. You do not even have to worry 
about making a mistake, and accidently modifying it.

Imagine if 'my programming language', said to you, we don't have 
the keyword 'const'. If you want it const, then don't change it. 
Or, put the function in its own module, so that it can't be 
changed.

So I do not see the difference, between the benefit of having 
const, and the benefit of having private(this). Both provide 
static verification of your program. There is no downside to 
that, IMO.

Obtaining static verification makes for a worthwhile addition to 
the language, IMO. It's not just some fad I want to bring into D, 
cause other languages have it.

I'm not sure this re-framing of my argument will help, but, this 
is my last hoorahhh on this topic ;-)

// ---
module test;
@safe:

import std;

void main()
{
     printString("Hello World!");
}

void printString(const string str)
{
     writeln(str);
}

// ----



More information about the Digitalmars-d mailing list