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

ProtectAndHide ProtectAndHide at gmail.com
Mon Feb 6 07:56:59 UTC 2023


On Sunday, 5 February 2023 at 23:53:35 UTC, Ali Çehreli wrote:
>
> I understand disabling the programmer to do certain things are 
> beneficial e.g. to prevent bugs but those above can all be seen 
> as features. What is so terrible about giving the programmer 
> those powers?
>
> Ali

Interestingly, in Swift, you cannot (as far as I can tell) 
disable init() like you can in D. Thus you cannot prevent the 
programmer from creating an instance of a class.

And you can forget about 'C# like' static classes in Swift also.

That leaves the question of why 'any' programming language would 
allow you to instantiate a class that has only static members. 
What exactly are you going to do with that instance? (i.e. there 
are no 'instance' members, just 'type' members).

At least in D you CAN disable the initialiser with @disable.

But even after you've done that, the compiler still:
- allows you to declare a variable of that type
- allows you to declare an array with elements of that type
- allows you to use that type as a type argument
- allows you to use that type as a parameter
- allows you to use that type as a return type

So, getting back to your statement.. under what circumstance 
would a programmer want the 'power' to do any or all of this 
things, if the programmer has explicately disabled the 
initialiser, and the type has only static 'type' members?



More information about the Digitalmars-d-learn mailing list