Using in one class a constant defined in another class

chopchop chopchop_fake at gmail.com
Tue Nov 16 18:12:34 UTC 2021


Hi,

I have a class Ground which defines some playground constants (at 
least constant to the class):
```
     class Ground
     {
         immutable WALL = -2;
         immutable playgroundWidth = 77;
         immutable playgroundHeight = 22;

...
}
```
Now, in another class "Player", I would like to use those 
playground constants:
```
     import ground;
     class Player
     {
         this()
         {
             x = Ground::playgroundWidth/2;
             y = Ground::playgroundHeight/2;
         }
     ...
     }
```
I used the "::" notation as a reference to C++, but obviously 
here it does not compile:
|Error: need `this` for `playgroundWidth` of type 
`immutable(int)`|

A class must have a "new" initialization in dlang, alright. But 
then I replaced immutable by enum in the playgroundWidth 
declaration, and it compiles fine with:
```
         enumplaygroundWidth = 77;
...
         x = Ground.playgroundWidth/2;
```
My question is : Is enum declaration the right way to go? Or did 
I just find a trick around the problem? Could not find anything 
in the documentation. Why enum works and immutable does not?

thanks




More information about the Digitalmars-d-learn mailing list