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

torhu torhu at yahoo.com
Fri Jan 20 15:01:26 UTC 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));
> ```

But why not have drawLine just be a free function?

```
import bluepandastuff.algo;

auto canvas = new Canvas();

drawLine(canvas, Pos(5, 3), Pos(7, 9));

// Or, using UFCS:
canvas.drawLine(Pos(5, 3), Pos(7, 9));

```

I turned Pos into a struct, seems like a typical value type to me.


More information about the Digitalmars-d-learn mailing list