Using in one class a constant defined in another class

Adam D Ruppe destructionator at gmail.com
Tue Nov 16 18:24:29 UTC 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.


More information about the Digitalmars-d-learn mailing list