Non-ugly ways to implement a 'static' class or namespace?
evilrat
evilrat666 at gmail.com
Fri Jan 20 13:38:47 UTC 2023
On Friday, 20 January 2023 at 13:17:05 UTC, Ruby The Roobster
wrote:
> On Friday, 20 January 2023 at 13:03:18 UTC, thebluepandabear
> wrote:
>> ll
>>> a function without instantiating said class, as functions act
>>> on the class object.
>>
>> Ok, thanks.
>>
>> I think D should implement something similar to `static class`
>> but I doubt it will happen.
>
> D isn't Java, and never will be. If you want similar
> functionality, you put the functions in a separate file, and
> add the line to the top of it:
>
> ```d
> module modulename;
> ```
>
> and title the file modulename.d. Then you can use this module
> as a .class file in java by adding
>
> ```d
> import modulename;
> ```
>
> to the file that uses it.
Also there is various import options such as renamed import or
static import(doesn't add module to a scope thus requiring to
fully qualify it)
static import, can be used to somewhat mimic namespaces, more
complex scenario would be making a module consisting of public
imports to group things together, but I don't think it is common
in D.
https://dlang.org/spec/module.html#static_imports
```d
static import std.stdio;
void main()
{
// nope, this function will not be resolved, compilation error
// wrtiteln("hello");
// ok
std.stdio.writeln("hello");
}
```
More information about the Digitalmars-d-learn
mailing list