Non-ugly ways to implement a 'static' class or namespace?

ProtectAndHide ProtectAndHide at
Sun Feb 5 10:20:14 UTC 2023

On Monday, 30 January 2023 at 21:54:49 UTC, thebluepandabear 
> ...
> Interesting, so maybe there is a use case for a purely static 
> type or namespace?
> The standard library as well uses `final abstract class` a 
> couple of times, which can also be thought as a type of 
> namespace.
> All these 'hacks' to workaround a namespace-like feature are 
> ... interesting... So maybe such a feature would help the 
> language?
> Just askin questions!

Yes, a C# like static class would be nice in D:

(i.e. you mark it as static, or whatever, and all the following 
then applies:
- Is sealed
- Cannot be instantiated or contain Instance Constructors.
- must contain only static members.

btw, it seems 'static' applied to class at the module level, in 
D, means nothing at all??

btw. Discovered that D has support for static constructors, which 
I didn't know.

Below is not that 'ugly', really, but I certainly prefer the 
clean, C# way (i.e. just mark it as static, and that's that).

// cannot inherit from, since it is final.
// cannot instantiate it with 'new', since it is annotated with 
//  NOTE: static here is meaningless and can be removed.
static final class Algo
     @disable this();

     static this()
         Message =  "Hello!";


     string Message;

     void drawLine() {};

More information about the Digitalmars-d-learn mailing list