January 27
On Saturday, 27 January 2024 at 19:58:55 UTC, Jordan Wilson wrote:
>
> ..
> I believe we are now in the "there is nothing more to be said" territory (just for the record, I think we both agree the feature is good, I just don't think the feature is necessary at all...nice-to-have at best. I suspect we'll agree to disagree).
>
> Jordan

It *is* necessary, if you are like me and want to design types upfront so you don't have to read all the code in the module to see which parts of that type are touched outside of the type, and which are not.

class C
{
    private int x;

    private(this):
        int y = 1;
        int w, z;

    public void test()
    {
      y = y + 4;
    }

    public int getY()
    {
        return y;
    }
}

unittest
{
    auto c = new C();

    import std;

    writeln(__traits(getVisibility, C.y));
    static assert(__traits(getVisibility, C.y) == "private(this)");

    c.x++; // allowed
    //c.y++; // not allowed
    //c.w++; // not allowed
    //c.z++; // not allowed

    c.test;
    writeln(c.getY);
}

January 28

On Thursday, 25 January 2024 at 15:03:41 UTC, Max Samukha wrote:

>

On Monday, 22 January 2024 at 23:28:40 UTC, Jonathan M Davis wrote:

>

Of course, ultimately, different programmers have different preferences, and none of us are going to be happy about everything in any language.

It's not only about preferences. The feature is inconsistent with how 'invariant' and 'synchronized' are specified. They imply class-instance-level private, while the language dictates module-level. Consider:

synchronized class C
{
    private int x;
    private int y;

    invariant () { assert (x == y); }

    static void foo(C c)
    {
        // mutate c
    }
}

Same thing. Yet would still break with some sort of "class-only private"

the unittest case is also similar -- what happens if you put the unittest next to the function being tested? It's now in the class, so it can access "true" private data. Same problems, this even can happen in Java. I don't see what the difference is. Same code, same file, just in a different spot? Seems more like you just need to... not do that.

-Steve

January 28

On Sunday, 28 January 2024 at 04:23:06 UTC, Steven Schveighoffer wrote:

>

..
the unittest case is also similar -- what happens if you put the unittest next to the function being tested? It's now in the class, so it can access "true" private data. Same problems, this even can happen in Java. I don't see what the difference is. Same code, same file, just in a different spot? Seems more like you just need to... not do that.

-Steve

There certainly are things you should not do.

There are also things you want the compiler to stop you doing.

And when you use a programming language, where mutable state automatically, by default, and at all times, leaks out of the type into the rest of the module, you may really appreciate the compilers help sometimes.

That's where private(this) comes in handy.

Now this example is pretty small, but imagine debugging this when foo() is all the way down on line 3546!

The intent should be right there in the design of the type. Intent should not be subject to analysis of all the code in the module.

No need to debug this program below, since it will not even compile.

module test;
@safe:

import std;

    class C
    {
        private(this) int x; // intent: other code in this module cannnot mutate this.
        private(this) int y; // intent: other code in this module cannnot mutate this.

        invariant () { assert (x == y); }

        void modifyX() {...}
        void modifyY() {...}
    }


    void foo(C c)
    {
        c.x = 10; // compiler will not compile this code.
        c.modifyX();
    }
January 28

On Sunday, 28 January 2024 at 04:47:30 UTC, FairEnough wrote:

>

module test;
@safe:

import std;

    class C
    {
        private(this) int x; // intent: other code in this module cannnot mutate this.
        private(this) int y; // intent: other code in this module cannnot mutate this.

        invariant () { assert (x == y); }

        void modifyX() {...}
        void modifyY() {...}
    }


    void foo(C c)
    {
        c.x = 10; // compiler will not compile this code.
        c.modifyX();
    }

Thank you for posting a very informative example, i think not just myself but everyone else on this forum had completely forgot how class private is supposed to work. In spite of the 1000 or so posts you've made about it.

Keep up the good work!

January 28

On Sunday, 28 January 2024 at 04:47:30 UTC, FairEnough wrote:

So 60 some odd posts later...

If you want to beat this pile of mush further, please do it in a new thread. Going forward, any posts I see about private-to-the-module which are off topic in any given thread will be happily deleted. You've hijacked enough of them.

Thanks!

January 28

On Sunday, 28 January 2024 at 15:31:24 UTC, Mike Parker wrote:

>

On Sunday, 28 January 2024 at 04:47:30 UTC, FairEnough wrote:

So 60 some odd posts later...

If you want to beat this pile of mush further, please do it in a new thread. Going forward, any posts I see about private-to-the-module which are off topic in any given thread will be happily deleted. You've hijacked enough of them.

Thanks!

Well I did specifcally say 55+ posts ago that this topic really should be discussed elsewhere.

But people continued to discuss it anyway - but only my responses raised your annoyance?

Do you mean you would actually allow a thread to discuss this topic, if that were the topic of that thread?

Such threads always get hijacked by irritant people completely opposed to this idea, and you end up killing those threads too.

January 28

On Sunday, 28 January 2024 at 15:31:24 UTC, Mike Parker wrote:

>

On Sunday, 28 January 2024 at 04:47:30 UTC, FairEnough wrote:

So 60 some odd posts later...

If you want to beat this pile of mush further, please do it in a new thread. Going forward, any posts I see about private-to-the-module which are off topic in any given thread will be happily deleted. You've hijacked enough of them.

Thanks!

Off topic certainly, but a 'pile of mush'?

January 28

On Sunday, 28 January 2024 at 15:31:24 UTC, Mike Parker wrote:

>

On Sunday, 28 January 2024 at 04:47:30 UTC, FairEnough wrote:

So 60 some odd posts later...

If you want to beat this pile of mush further, please do it in a new thread. Going forward, any posts I see about private-to-the-module which are off topic in any given thread will be happily deleted. You've hijacked enough of them.

Thanks!

No more comments about this in this thread. I promise.

For those who don't consider it 'a pile of mush', here is where it 'can' be discussed (freely I hope):

https://github.com/orgs/opendlang/discussions/11

January 28

On Sunday, 28 January 2024 at 22:31:59 UTC, FairEnough wrote:

>

No more comments about this in this thread. I promise.

Thank you.

>

For those who don't consider it 'a pile of mush', here is where it 'can' be discussed (freely I hope):

It can be freely discussed here, too. Just please stop hijacking threads to do it.

As for "pile of mush", it's what the dead horse has been beaten into at this point.

January 29
On Sunday, 21 January 2024 at 07:11:50 UTC, Jordan Wilson wrote:
> On Saturday, 20 January 2024 at 22:53:15 UTC, privateWHAT wrote:
>> On Thursday, 18 January 2024 at 07:19:19 UTC, Mike Parker wrote:
>>>
>>> * establish support for fleshing out ideas before a DIP is even written
>>>
>>
>> It's 2024. That should have been the principle a decade ago
>>
>> Remember how the so called 'discussions' about the 'privateThis' concept always got heaped on. People just wanted to shut it down rather that discuss it. 'Go write a DIP' was the response...remember.
>
> The class vs module level of encapsulation was fleshed out a lot in the forums awhile back. I think it's fair to say most people where happy (or neutral) with the status quo, and were not convinced by the pro-class-level arguments.
>
> I also suspect those that did prefer class level private (I believe this is what Atila prefers), it's not high on their list of priorities.

I like private as it is now. Especially because if anyone wants it to be "class-private", they can do that anyway: write a class per module like one has to in Java.

My 2 centimes (cos I live in Switzerland) is that if you're worried about too much code having access to your private functions, your module is probably too large.