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

GrimMaple grimmaple95 at gmail.com
Wed Feb 8 10:18:29 UTC 2023


On Monday, 23 January 2023 at 01:28:30 UTC, Ali Çehreli wrote:

> I agree with Adam here. The language should provide solutions 
> and the programmer should pick appropriate ones.

Then just add `static class`, as it will provide a solution 
without robbing you of another solution. You still can put 
symbols in a module even if `static class` is a thing. `static 
class` doesn't break existing solutions, it just adds things on 
top.


> OOP is always a solution where it makes sense. I've been very 
> happy with writing free-standing functions and dumb data types 
> that the functions operate on... until... some invariant 
> matters. Then I make the function a member, etc.
>
> Perhaps because I don't buy into the "everything is a class" 
> anymore, I am very happy with D's approach. Mostly structs and 
> functions for me. But I use classes as well when they make 
> sense.

Good. Now, let people have OOP and "everything is a class" if 
they want to.

> Having said all that, I realize that your asking specifically 
> for static classes made me think of a solution around classes. 
> However, doesn't D has the equivalent in structs? Isn't the 
> following what you are looking for?
>
> struct Algo {
>     static void drawLine(Canvas c, Pos from, Pos to) { ...... };
> }
>
> Now the user is forced to use it like this:
>
>   Algo.drawLine(new Canvas(), new Pos(5, 3), new Pos(7, 9));
>
> or this:
>
>   Algo().drawLine(/* ... */);
>
> but that can be @disabled.

I think it's better to have a widespread commonly known solution 
than hacks that you have to specifically look for. D is guilty 
with having too many hacks that you have to specifically learn 
about.




More information about the Digitalmars-d-learn mailing list