January 30, 2023
On Monday, 30 January 2023 at 08:20:21 UTC, ProtectAndHide wrote:

> But there is always 'an ugly way' to do it too:
> http://www.cs.rit.edu/~ats/books/ooc.pdf

Yep, but it’s not OOP per se, just a form of lowering. First CPP implementation was a cross-compiled one.
January 30, 2023
On 1/29/23 22:09, RTM wrote:
> On Saturday, 28 January 2023 at 23:19:35 UTC, ProtectAndHide wrote:
>
>> That is, you can do OOP without classes
>
> How so?

OOP is about putting objects (data) and behavior (functions) together.

> Every OOP definition includes classes

OOP is possible in C, which does not have classes:

  void sing(Animal * animal);

That top-level sing function will do something like this:

  animal->sing(animal);

Every object has their own sing function pointer as a member. (Or one can go to a vtbl pointer approach with different pros and cons; I used all of this as an interview topic at some point.)

Programming languages just make it easy.

> (encapsulation + inheritance).

Encapsulation is available even in C as well. Inheritance can be achieved manually.

And I used C just because it does not provide any special OOP features.

Ali

January 30, 2023

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

>
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.

Interesting solution if you put static: in there.

>

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

Why do you want a type?

>

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

Use a struct and put static: after the opening brace. That's what GC is in core.memory.

January 30, 2023
>

Use a struct and put static: after the opening brace. That's what GC is in core.memory.

Using a struct for a purely static type would still allow the user to create instances of that struct. To bypass that, you'd have to disable the default constructor -- that then becomes ugly, hackish code.

January 30, 2023

On Monday, 30 January 2023 at 21:50:03 UTC, thebluepandabear wrote:

> >

Use a struct and put static: after the opening brace. That's what GC is in core.memory.

Using a struct for a purely static type would still allow the user to create instances of that struct. To bypass that, you'd have to disable the default constructor -- that then becomes ugly, hackish code.

Looking at the GC code found @ https://github.com/dlang/dmd/blob/master/druntime/src/core/memory.d, it seems like they have disabled the default constructor:

struct GC
{
    @disable this();
    ...
}

Interesting, so maybe there is a use case for a purely static type or namespace?

The standard library as well uses final abstract class a couple of times, which can also be thought as a type of namespace.

All these 'hacks' to workaround a namespace-like feature are ... interesting... So maybe such a feature would help the language?

Just askin questions!

January 30, 2023
>

Why do you want a type?

I want a type because it gives clear context as to what family the method(s) belongs to, and helps make the code more idiomatic and easy to understand.

February 05, 2023
On Monday, 30 January 2023 at 21:54:49 UTC, thebluepandabear wrote:
>
> ...
> Interesting, so maybe there is a use case for a purely static type or namespace?
>
> The standard library as well uses `final abstract class` a couple of times, which can also be thought as a type of namespace.
>
> All these 'hacks' to workaround a namespace-like feature are ... interesting... So maybe such a feature would help the language?
>
> Just askin questions!

Yes, a C# like static class would be nice in D:

(i.e. you mark it as static, or whatever, and all the following then applies:
- Is sealed
- Cannot be instantiated or contain Instance Constructors.
- must contain only static members.

btw, it seems 'static' applied to class at the module level, in D, means nothing at all??

btw. Discovered that D has support for static constructors, which I didn't know.

Below is not that 'ugly', really, but I certainly prefer the clean, C# way (i.e. just mark it as static, and that's that).

// cannot inherit from, since it is final.
// cannot instantiate it with 'new', since it is annotated with @disable
//
//  NOTE: static here is meaningless and can be removed.
static final class Algo
{
    @disable this();

    static this()
    {
        Message =  "Hello!";
    }

    static:

    string Message;

    void drawLine() {};
}

February 05, 2023
> {
>     @disable this();
>
>     static this()
>     {
>         Message =  "Hello!";
>     }
>
>     static:
>
>     string Message;
>
>     void drawLine() {};
> }

It's not a terrible workaround to be honest.

`static class` does have a use when it's nested, so it might create some conflicts if such a feature (like C# `static class`) were added.

Due to the mindset of the current maintainers of the language, I doubt we will see such a thing. Maybe in 10-20 years something will change and the language will add a static class or namespace feature, for now we'll have to deal with modules or using your way of creating a `static class`.


February 05, 2023
On Sunday, 5 February 2023 at 10:51:51 UTC, thebluepandabear wrote:
>> {
>>     @disable this();
>>
>>     static this()
>>     {
>>         Message =  "Hello!";
>>     }
>>
>>     static:
>>
>>     string Message;
>>
>>     void drawLine() {};
>> }
>
> It's not a terrible workaround to be honest.
>
> `static class` does have a use when it's nested, so it might create some conflicts if such a feature (like C# `static class`) were added.
>
> Due to the mindset of the current maintainers of the language, I doubt we will see such a thing. Maybe in 10-20 years something will change and the language will add a static class or namespace feature, for now we'll have to deal with modules or using your way of creating a `static class`.

I don't like it when people see modules as a replacement for a namespace/static class, when that's not the case.

Rust has modules. It also has namespaces.

C++ will be getting modules, it also has namespaces.

When dealing with contexts, or for when you want a clear context in your codebase, namespaces can be a life saver, we've seen it used in the D library, so there's no excuse for why this shouldn't be added, in my opinion.
February 05, 2023
On Sunday, 5 February 2023 at 10:51:51 UTC, thebluepandabear wrote:
>
> It's not a terrible workaround to be honest.
> ....

The 'terrible' part is this:

- the compiler will allow you to declare a variable of type Algo
- the compiler will allow you to declare an array with elements of type Algo
- the compiler will allow you to use Algo as a type argument
- the compiler will allow you to use Algo as a parameter
- the compiler will allow you to use Algo as a return type