www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using in one class a constant defined in another class

reply chopchop <chopchop_fake gmail.com> writes:
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
Nov 16 2021
next sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Tuesday, 16 November 2021 at 18:12:34 UTC, chopchop wrote:
     class Ground
     {
         immutable WALL = -2;
immutable isn't automatically static, so you'd want to use `static immutable` to share the value across all instances. (you can actually initialize immutable things to different values in a constructor, then it is locked for the lifetime of the instance. Being static of course means no more per-instance values.)
         x = Ground.playgroundWidth/2;
Yes, D always uses `.` including places where C uses `->` and C++ uses `::`. Almost always plain dot in D.
 anything in the documentation. Why enum works and immutable 
 does not?
enum only exists at compile time, so it also implies static. The difference between static immutable and enum is that enum acts like a literal - it is an rvalue and has no address. Only the compiler knows about it. static immutable actually does have a memory address so you can take a pointer to it.
Nov 16 2021
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 16 November 2021 at 18:12:34 UTC, chopchop wrote:
 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)`|
The reason you get this error is that you have declared `playgroundWidth` and `playgroundHeight` as instance variables of `Ground`. Of course, in order to access an instance variable, you need to have an instance. When the compiler says "need `this`", it means you need an instance of the class. The correct fix is to declare the variables as `static`: ```d class Ground { static immutable WALL = -2; static immutable playgroundWidth = 77; static immutable playgroundHeight = 22; } ``` As far as I know, this works the same way in D as it does in C++. Here's an example of C++ code that has the same error: ```c++ class C { public: int x = 42; }; int main() { // error: invalid use of non-static data member 'C::x' int y = C::x; return 0; } ``` Interactive version: https://godbolt.org/z/a5xvzooev
 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?
The version with the enum declarations works because enum declarations are always considered `static`.
Nov 16 2021
parent chopchop <chopchop_fake gmail.com> writes:
Thanks guys, it is very clear now.
Nov 17 2021