digitalmars.D.learn - Using in one class a constant defined in another class
- chopchop (42/42) Nov 16 2021 Hi,
- Adam D Ruppe (14/20) Nov 16 2021 immutable isn't automatically static, so you'd want to use
- Paul Backus (33/67) Nov 16 2021 The reason you get this error is that you have declared
- chopchop (1/1) Nov 17 2021 Thanks guys, it is very clear now.
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
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
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/a5xvzooevMy 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