On Monday, 29 January 2024 at 12:34:05 UTC, Atila Neves wrote:
..
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.

I'd argue:
(1) this module is not too large. (sure its just for demo the concept though) (2) private(this) make complete sense here. There is zero need for the unittest of have access to count. So why not make that part of the Widget class design. I don't see any downside whatsoever. Is it absolutely necessary, well, no. But I like the design clarity it offers.

module test;

class Widget
{
  public:
    this() { ++count; }
    ~this() { --count; }

    int numWidgets(){ return count; }

  private(this):
    static int count;
};

unittest
{
    Widget w = new Widget, x = new Widget;
    assert(w.numWidgets() == 2);
}

Reply via email to