Jump to page: 1 24  
Page
Thread overview
Non-ugly ways to implement a 'static' class or namespace?
Jan 20
evilrat
Jan 20
Hipreme
Jan 20
torhu
Jan 22
ryuukk_
Jan 25
ryuukk_
6 days ago
Salih Dincer
6 days ago
RTM
6 days ago
ProtectAndHide
6 days ago
RTM
5 days ago
Ali Çehreli
5 days ago
Nick Treleaven
1 hour ago
ProtectAndHide
33 minutes ago
thebluepandabear
28 minutes ago
thebluepandabear
January 20

Hi,

In Java/C# you can create purely static classes.

These are classes whose methods are all static, the classes cannot be derived from or instantiated:

static class Algo {
    void drawLine(Canvas c, Pos from, Pos to) { ...... };
}

Class in use:

Algo.drawLine(new Canvas(), new Pos(5, 3), new Pos(7, 9));

This type of semantics is not possible in D, which sucks.

After scouring the forums, the only workaround seems to be the following:

final abstract class Algo {
    void drawLine(Canvas c, Pos from, Pos to) { ...... };
}

This solution seems like a bit of a hack, which is why I don't like it.

Alternatively you could create a module, but then it would just be a function without a clear type.

Is anyone aware of a non-ugly way to implement a 'static' class or namespace?

Regards,
thebluepandabear

January 20

On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear wrote:

>

Hi,

In Java/C# you can create purely static classes.

These are classes whose methods are all static, the classes cannot be derived from or instantiated:

static class Algo {
    void drawLine(Canvas c, Pos from, Pos to) { ...... };
}

Class in use:

Algo.drawLine(new Canvas(), new Pos(5, 3), new Pos(7, 9));

This type of semantics is not possible in D, which sucks.

After scouring the forums, the only workaround seems to be the following:

final abstract class Algo {
    void drawLine(Canvas c, Pos from, Pos to) { ...... };
}

This solution seems like a bit of a hack, which is why I don't like it.

Alternatively you could create a module, but then it would just be a function without a clear type.

Is anyone aware of a non-ugly way to implement a 'static' class or namespace?

Regards,
thebluepandabear

There is no way to implement that functionality in D. final means that the class cannot be extended, and abstract requires that only an extension of said class can be instantiated. However, unlike in Java and C#, you cannot call a function without instantiating said class, as functions act on the class object.

January 20

On Friday, 20 January 2023 at 12:55:37 UTC, Ruby The Roobster wrote:

>

On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear ...

There is no way to implement that functionality in D. final means that the class cannot be extended, and abstract requires that only an extension of said class can be instantiated. However, unlike in Java and C#, you cannot call a function without instantiating said class, as functions act on the class object.

Also, there is a static keyword, but a static class can be instantiated as a member of the external class.

January 20

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.

January 20

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:

module modulename;

and title the file modulename.d. Then you can use this module as a .class file in java by adding

import modulename;

to the file that uses it.

January 20

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:

module modulename;

and title the file modulename.d. Then you can use this module as a .class file in java by adding

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

static import std.stdio;

void main()
{
  // nope, this function will not be resolved, compilation error
  // wrtiteln("hello");

  // ok
  std.stdio.writeln("hello");
}
January 20

On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear wrote:

>

Hi,

In Java/C# you can create purely static classes.

These are classes whose methods are all static, the classes cannot be derived from or instantiated:

static class Algo {
    void drawLine(Canvas c, Pos from, Pos to) { ...... };
}

There are 2 solutions for that. One involved doing a private implementation:

module drawer.impl;
void drawLine(...)

Then, you create another file:

module drawer;
public import Algo = drawer.impl;

After that, you can use it as Algo.drawLine.

With a single file, you can do:


final class Algo
{
    @disable this();
    static:
    void drawLine(...){}
}
January 20

On Friday, 20 January 2023 at 13:38:47 UTC, evilrat wrote:

>

On Friday, 20 January 2023 at 13:17:05 UTC, Ruby The Roobster ...
[snip]

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

static import std.stdio;

void main()
{
  // nope, this function will not be resolved, compilation error
  // wrtiteln("hello");

  // ok
  std.stdio.writeln("hello");
}

I never knew that there even WAS a static import option. Good to know.

January 20

On Friday, 20 January 2023 at 13:38:52 UTC, Hipreme wrote:

>

On Friday, 20 January 2023 at 11:28:23 UTC, thebluepandabear wrote:
...
[snip]

With a single file, you can do:


final class Algo
{
    @disable this();
    static:
    void drawLine(...){}
}

This also works, but it dissimilar to Java in that in Java, each class gets its own file.

January 20

On 1/20/23 6:28 AM, thebluepandabear wrote:

>

This type of semantics is not possible in D, which sucks.

Well, static methods do exactly this.

If you want to disable class creation, then use @disable this();, if you want to make all methods static, put static: at the top of the class.

Note that at this point, the class becomes a namespace. You can use a template as a namespace as well, though it can be a bit ugly:

template Algo_ns()
{
   void drawLine(Canvas c, Pos from, Pos to) {...}
}

// need this to avoid the instantiation syntax
alias Algo = Algo_ns!();

The benefit here is that other things that classes do (generate typeinfo, add to the type system, etc.) would be wasted, so this is not done for a template.

-Steve

« First   ‹ Prev
1 2 3 4