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

thebluepandabear therealbluepandabear at protonmail.com
Sun Feb 5 10:57:11 UTC 2023


On Sunday, 5 February 2023 at 10:51:51 UTC, thebluepandabear 
wrote:
>> {
>>     @disable this();
>>
>>     static this()
>>     {
>>         Message =  "Hello!";
>>     }
>>
>>     static:
>>
>>     string Message;
>>
>>     void drawLine() {};
>> }
>
> It's not a terrible workaround to be honest.
>
> `static class` does have a use when it's nested, so it might 
> create some conflicts if such a feature (like C# `static 
> class`) were added.
>
> Due to the mindset of the current maintainers of the language, 
> I doubt we will see such a thing. Maybe in 10-20 years 
> something will change and the language will add a static class 
> or namespace feature, for now we'll have to deal with modules 
> or using your way of creating a `static class`.

I don't like it when people see modules as a replacement for a 
namespace/static class, when that's not the case.

Rust has modules. It also has namespaces.

C++ will be getting modules, it also has namespaces.

When dealing with contexts, or for when you want a clear context 
in your codebase, namespaces can be a life saver, we've seen it 
used in the D library, so there's no excuse for why this 
shouldn't be added, in my opinion.


More information about the Digitalmars-d-learn mailing list