Local static class fields

Paul Backus snarwin at gmail.com
Tue Aug 13 04:43:29 UTC 2019


On Monday, 12 August 2019 at 22:48:43 UTC, Bert wrote:
> Making a field static is effectively a global variable to the 
> class.
>
> I have a recursive class structure(think of a graph or tree) 
> and I need to keep a global state for it, but this state 
> actually needs to be different for each tree object. The reason 
> for this is that structurally it will not change per tree and 
> if it it is not static then it wastes space unnecessarily.
>
> [...]
>
> Is there any way to do this more naturally in D?

It seems to me like the obvious solution is to use two different 
classes, one to store the global state, and one to store the 
individual objects in your structure. For example:

class Tree {
     State state;
     Node root;
}

class Node {
     Node[] children;
}


More information about the Digitalmars-d-learn mailing list