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

thebluepandabear therealbluepandabear at protonmail.com
Mon Jan 30 21:54:49 UTC 2023


On Monday, 30 January 2023 at 21:50:03 UTC, thebluepandabear 
wrote:
>> Use a struct and put `static:` after the opening brace. That's 
>> what GC is in core.memory.
>
> Using a `struct` for a purely static type would still allow the 
> user to create instances of that `struct`. To bypass that, 
> you'd have to disable the default constructor -- that then 
> becomes ugly, hackish code.

Looking at the GC code found @ 
https://github.com/dlang/dmd/blob/master/druntime/src/core/memory.d, it seems like they have disabled the default constructor:

```D
struct GC
{
     @disable this();
     ...
}
```

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!



More information about the Digitalmars-d-learn mailing list