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

Mike Parker aldacron at gmail.com
Mon Jan 23 03:55:41 UTC 2023


On Monday, 23 January 2023 at 00:36:36 UTC, thebluepandabear 
wrote:

>
> I haven't been programming for a long time, but most of the 
> other languages I used had such a namespace feature. Kotlin has 
> something called an `object` which is essentially a namespace 
> and it is great. The benefits of adding a namespace-like 
> feature outweigh its costs, imo.

If you really want to, you can mostly force a namespace use like 
this:

```
// mylib/package.d
module mylib;
public static import mylib.impl.funcs;

// mylib/impl/funcs.d
module mylib.impl.funcs;

void foo() { }
```

Now when users import mylib, the public static import means hey 
call mylib.foo. Just don't bother documenting the impl subpackage 
and only those who look at the source will even know it exists.

I went through this same process when I first came to D years 
ago. D's modules *are* namespaces, and I wanted a way to force 
them. Eventually, I got over it. There's no reason to force a 
namespace. Namespaces are intended to disambiguate conflicting 
symbols. So let the users use them that way. There's no need to 
force them to type out the namespace all the time. It's certainly 
not an OOP vs. procedural issue, as namespaces have nothing to do 
with OOP.


More information about the Digitalmars-d-learn mailing list