www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Proper desctructor for an class containing dynamic array of objects

reply Mike <externc aol.com> writes:
Hi,

my name is Mike and I'm new to D (coming from a Javabackground) 
and for fun I'm trying to learn D now.
I created a simple class

class Block {

     int a, b;
     this() {}

}

And now I have a dynamic array of objects of this class in 
another class:

class Foo {

      Block[] array  = new Block[](10);

      this() {
        for (int i = 0; i < array.length; i++) {
           array[i] = new Block();
        }
      }
}

How would a proper destructor of class Foo look like?
Is it enough to set "array" to null? Or do I have to set every 
element of the array to null and then the array, or nothing of 
that at all because the garbage collecter collects it, if the 
reference to Foo is set to null?
Jun 13 2019
next sibling parent kinke <noone nowhere.com> writes:
On Thursday, 13 June 2019 at 16:08:52 UTC, Mike wrote:
 or nothing of that at all because the garbage collecter 
 collects it, if the reference to Foo is set to null?
That. [The init loop can be shortened to `foreach (ref b; array) b = new Block();`.]
Jun 13 2019
prev sibling next sibling parent Rumbu <rumbu rumbu.ro> writes:
On Thursday, 13 June 2019 at 16:08:52 UTC, Mike wrote:
 How would a proper destructor of class Foo look like?
 Is it enough to set "array" to null? Or do I have to set every 
 element of the array to null and then the array, or nothing of 
 that at all because the garbage collecter collects it, if the 
 reference to Foo is set to null?
Nothing at all, GC will take care. As long as your Block class doesn't create any system resources that must be discarded.
Jun 13 2019
prev sibling parent reply Marco de Wild <mdwild sogyo.nl> writes:
On Thursday, 13 June 2019 at 16:08:52 UTC, Mike wrote:
 Hi,

 my name is Mike and I'm new to D (coming from a Javabackground) 
 and for fun I'm trying to learn D now.
 I created a simple class

 class Block {

     int a, b;
     this() {}

 }

 And now I have a dynamic array of objects of this class in 
 another class:

 class Foo {

      Block[] array  = new Block[](10);

      this() {
        for (int i = 0; i < array.length; i++) {
           array[i] = new Block();
        }
      }
 }

 How would a proper destructor of class Foo look like?
 Is it enough to set "array" to null? Or do I have to set every 
 element of the array to null and then the array, or nothing of 
 that at all because the garbage collecter collects it, if the 
 reference to Foo is set to null?
Opposed to Java, D's member variables are static initialised. There is one default value for "array" that is shared between all instances of Foo. This allows for compiler optimisations of primitives (baking in a int value of 5 into the class definition, but gives unexpected behaviour for reference types. https://run.dlang.io/is/UNp5Al In this example, two instances of Foo are created. The constructor is run both times. However, they act on the same array object. So initialisation of the second Foo will modify references data of the first Foo. As a check, the first object of both Foos are compared by reference. Java allows you to define a block of code in your class that is run on construction, e.g. public class JavaClass { { doStuff(); } int x = initializeX(); } It makes for fun questions on things like OCA certificates. However, D doesn't allow that. All initialisation code is placed in the constructor, e.g. this() { array = new Block[](10); // ... } (Of course you can call other methods from there. In fact, constructor rules are slightly relaxed compared to Java.)
Jun 14 2019
parent reply rumbu <rumbu rumbu.ro> writes:
On Friday, 14 June 2019 at 07:52:24 UTC, Marco de Wild wrote:
 On Thursday, 13 June 2019 at 16:08:52 UTC, Mike wrote:
 Opposed to Java, D's member variables are static initialised.
Is there any documentation about this? I find it unexpected.
Jun 14 2019
parent XavierAP <n3minis-git yahoo.es> writes:
On Friday, 14 June 2019 at 11:10:58 UTC, rumbu wrote:
 On Friday, 14 June 2019 at 07:52:24 UTC, Marco de Wild wrote:
 On Thursday, 13 June 2019 at 16:08:52 UTC, Mike wrote:
 Opposed to Java, D's member variables are static initialised.
Is there any documentation about this? I find it unexpected.
https://dlang.org/spec/class.html#static-constructor «All member initializations must be determinable by the compiler at compile time, hence there is no order-of-evaluation dependency for member initializations, and it is not possible to read a value that has not been initialized. Dynamic initialization is performed by a static constructor»
Jun 14 2019