Jump to page: 1 220  
Page
Thread overview
Non-ugly ways to implement a 'static' class or namespace?
Jan 20, 2023
thebluepandabear
Jan 20, 2023
Ruby The Roobster
Jan 20, 2023
Ruby The Roobster
Jan 20, 2023
thebluepandabear
Jan 20, 2023
Ruby The Roobster
Jan 20, 2023
evilrat
Jan 20, 2023
Ruby The Roobster
Jan 20, 2023
Hipreme
Jan 20, 2023
Ruby The Roobster
Jan 20, 2023
torhu
Jan 20, 2023
Ali Çehreli
Jan 20, 2023
H. S. Teoh
Jan 23, 2023
thebluepandabear
Jan 23, 2023
Adam D Ruppe
Jan 23, 2023
thebluepandabear
Jan 23, 2023
Mike Parker
Jan 25, 2023
thebluepandabear
Feb 10, 2023
Kagamin
Feb 10, 2023
Kagamin
Jan 23, 2023
Ali Çehreli
Feb 08, 2023
GrimMaple
Jan 22, 2023
ryuukk_
Jan 23, 2023
thebluepandabear
Jan 23, 2023
thebluepandabear
Jan 23, 2023
Mike Parker
Jan 25, 2023
ryuukk_
Jan 28, 2023
ProtectAndHide
Jan 29, 2023
Salih Dincer
Jan 30, 2023
thebluepandabear
Jan 30, 2023
RTM
Jan 30, 2023
ProtectAndHide
Jan 30, 2023
RTM
Jan 30, 2023
Ali Çehreli
Jan 23, 2023
thebluepandabear
Jan 30, 2023
Nick Treleaven
Jan 30, 2023
thebluepandabear
Jan 30, 2023
thebluepandabear
Feb 05, 2023
ProtectAndHide
Feb 05, 2023
thebluepandabear
Feb 05, 2023
thebluepandabear
Feb 05, 2023
Ali Çehreli
Feb 06, 2023
thebluepandabear
Feb 05, 2023
ProtectAndHide
Feb 05, 2023
ProtectAndHide
Feb 05, 2023
Ali Çehreli
Feb 06, 2023
thebluepandabear
Feb 06, 2023
Ali Çehreli
Feb 06, 2023
ProtectAndHide
Feb 06, 2023
thebluepandabear
Feb 06, 2023
Ali Çehreli
Feb 06, 2023
ProtectAndHide
Feb 07, 2023
ProtectAndHide
Feb 07, 2023
Ali Çehreli
Feb 06, 2023
Ali Çehreli
Feb 07, 2023
ProtectAndHide
Feb 07, 2023
Ali Çehreli
Feb 07, 2023
ProtectAndHide
Feb 08, 2023
thebluepandabear
Feb 08, 2023
thebluepandabear
Feb 06, 2023
ProtectAndHide
Jan 30, 2023
thebluepandabear
Feb 08, 2023
zjh
Feb 08, 2023
zjh
Feb 08, 2023
thebluepandabear
Feb 08, 2023
zjh
Feb 09, 2023
thebluepandabear
Feb 09, 2023
Adam D Ruppe
Feb 09, 2023
thebluepandabear
Feb 09, 2023
Adam D Ruppe
Feb 09, 2023
thebluepandabear
Feb 09, 2023
Guillaume Piolat
Feb 09, 2023
thebluepandabear
Feb 09, 2023
zjh
Feb 09, 2023
thebluepandabear
Feb 09, 2023
zjh
Feb 09, 2023
zjh
Feb 09, 2023
zjh
Feb 09, 2023
zjh
Feb 09, 2023
zjh
Feb 09, 2023
Ali Çehreli
Feb 09, 2023
ProtectAndHide
Feb 09, 2023
Hipreme
Feb 10, 2023
ProtectAndHide
Feb 09, 2023
Ali Çehreli
Feb 09, 2023
ProtectAndHide
Feb 10, 2023
Ali Çehreli
Feb 09, 2023
thebluepandabear
Feb 10, 2023
bachmeier
Feb 09, 2023
thebluepandabear
Feb 10, 2023
Ali Çehreli
Feb 10, 2023
bachmeier
Feb 10, 2023
ProtectAndHide
Feb 10, 2023
zjh
Feb 11, 2023
ProtectAndHide
Feb 10, 2023
zjh
Feb 10, 2023
zjh
Feb 10, 2023
zjh
Feb 10, 2023
Max Samukha
Feb 10, 2023
bachmeier
Feb 10, 2023
ProtectAndHide
Feb 10, 2023
thebluepandabear
Feb 10, 2023
thebluepandabear
Feb 11, 2023
ProtectAndHide
Feb 11, 2023
thebluepandabear
Feb 11, 2023
ProtectAndHide
Feb 11, 2023
ProtectAndHide
Feb 11, 2023
thebluepandabear
Feb 11, 2023
ProtectAndHide
Feb 10, 2023
thebluepandabear
Feb 11, 2023
ProtectAndHide
Feb 11, 2023
thebluepandabear
Feb 11, 2023
ProtectAndHide
Feb 11, 2023
thebluepandabear
Feb 13, 2023
Kagamin
Feb 13, 2023
ProtectAndHide
Feb 13, 2023
Kagamin
Feb 13, 2023
ProtectAndHide
Feb 14, 2023
Kagamin
Feb 14, 2023
ProtectAndHide
Feb 14, 2023
thebluepandabear
Feb 14, 2023
bachmeier
Feb 14, 2023
ProtectAndHide
Feb 15, 2023
thebluepandabear
Feb 15, 2023
thebluepandabear
Feb 15, 2023
Mike Parker
Feb 15, 2023
bachmeier
Feb 15, 2023
thebluepandabear
Feb 15, 2023
bachmeier
Feb 15, 2023
thebluepandabear
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
thebluepandabear
Feb 15, 2023
Mike Parker
Feb 15, 2023
Mike Parker
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
zjh
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
zjh
Feb 16, 2023
zjh
Feb 15, 2023
Mike Parker
Feb 15, 2023
zjh
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
bachmeier
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
ProtectAndHide
Feb 16, 2023
Mike Parker
Feb 16, 2023
Mike Parker
Feb 16, 2023
FeepingCreature
Feb 16, 2023
thebluepandabear
Feb 16, 2023
H. S. Teoh
Feb 16, 2023
ProtectAndHide
Feb 16, 2023
ProtectAndHide
Feb 16, 2023
bachmeier
Feb 16, 2023
ProtectAndHide
Feb 16, 2023
ProtectAndHide
Feb 17, 2023
zjh
Feb 17, 2023
zjh
Feb 17, 2023
zjh
Feb 17, 2023
RTM
Feb 17, 2023
RTM
Feb 17, 2023
ProtectAndHide
Feb 17, 2023
ProtectAndHide
Feb 17, 2023
RTM
Feb 17, 2023
RTM
Feb 17, 2023
ProtectAndHide
Feb 17, 2023
ProtectAndHide
Feb 18, 2023
ProtectAndHide
Feb 18, 2023
RTM
Feb 18, 2023
ProtectAndHide
Feb 18, 2023
zjh
Feb 18, 2023
RTM
Feb 18, 2023
ProtectAndHide
Feb 18, 2023
ProtectAndHide
Feb 18, 2023
ProtectAndHide
Feb 18, 2023
Adam D Ruppe
Feb 19, 2023
ProtectAndHide
Feb 19, 2023
ProtectAndHide
Feb 19, 2023
ProtectAndHide
Feb 19, 2023
thebluepandabear
Feb 19, 2023
thebluepandabear
Feb 17, 2023
ProtectAndHide
Feb 16, 2023
ProtectAndHide
Feb 16, 2023
thebluepandabear
Feb 16, 2023
ProtectAndHide
Feb 16, 2023
ProtectAndHide
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
ProtectAndHide
Feb 15, 2023
ProtectAndHide
Feb 20, 2023
forky
Feb 20, 2023
FeepingCreature
Feb 20, 2023
thebluepandabear
Feb 20, 2023
Mike Parker
January 20, 2023

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, 2023

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, 2023

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, 2023

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, 2023

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, 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:

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, 2023

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, 2023

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, 2023

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, 2023

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 5 6 7 8 9 10 11