chkaufmann 17 Posted June 17, 2020 In 10.3 (Rio), class constructors are implemented the same way as initialization sections of units. This means, I have no control about the order. So if I create an object of my class in an initialization section, I cannot be sure, that the class constructor for this class was already called. Just wondering: is this problem fixed in 10.4? Or is it still done the same way? Christian Share this post Link to post
Stefan Glienke 2002 Posted June 17, 2020 (edited) 28 minutes ago, chkaufmann said: So if I create an object of my class in an initialization section, I cannot be sure, that the class constructor for this class was already called According to what Allen wrote in 2009 that is not true. If there is a bug with that please link to the QP entry and/or code to repro wrong behavior. Edited June 17, 2020 by Stefan Glienke 2 Share this post Link to post
Mahdi Safsafi 225 Posted June 17, 2020 Class constructors are executed in the lexical order in which they implemented (not declared). Class destructor executed in the reverse order in which they implemented. NOTE: Many languages follow the same principle. 2 Share this post Link to post
Stefan Glienke 2002 Posted June 17, 2020 1 hour ago, Mahdi Safsafi said: NOTE: Many languages follow the same principle. Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem. 1 Share this post Link to post
Bill Meyer 337 Posted June 17, 2020 31 minutes ago, Stefan Glienke said: Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem. In my experience, the initialization section is dangerous. When you have a large legacy project with many unit dependency cycles, and begin working to clean up that issue, the changes in uses clauses may lead to reordering of the initialization sequence. I found it necessary to log the existing order, then replace initialization sections with InitializeUnit procedures called from an initialization manager. That let me proceed with the cleanup. Without that change, I soon had an app with a lower dependency cycle count, but which did not work. 1 Share this post Link to post
Mahdi Safsafi 225 Posted June 17, 2020 32 minutes ago, Stefan Glienke said: Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem. You're absolutely right Stefan. Particularly, the awesome D language has initialization and finalization sections, but it implements them in a very sexy way : Static constructors(initialization) are executed to initialize a module(unit)'s state. Static destructors(finalization) terminate a module's state. A module may have multiple static constructors and static destructors. The static constructors are run in lexical order, the static destructors are run in reverse lexical order. Non-shared static constructors and destructors are run whenever threads are created or destroyed, including the main thread. Shared static constructors are run once before main() is called. Shared static destructors are run after the main() function returns. But as you said, it can be a source of lot of bugs. 1 Share this post Link to post
Uwe Raabe 2057 Posted June 17, 2020 1 hour ago, Mahdi Safsafi said: the awesome D language Isn't that just a subset of Delphi? 1 Share this post Link to post
David Heffernan 2345 Posted June 17, 2020 D is a truly wonderful language, and Andrei Alexandrescu's book is by some distance the best programming book I have ever read. 2 Share this post Link to post
Mahdi Safsafi 225 Posted June 17, 2020 1 hour ago, Uwe Raabe said: Isn't that just a subset of Delphi? If it was true ... then we're definitely using Elphi 😉 3 Share this post Link to post
Mahdi Safsafi 225 Posted June 17, 2020 1 hour ago, David Heffernan said: D is a truly wonderful language, and Andrei Alexandrescu's book is by some distance the best programming book I have ever read. Indeed ! D official doc is also super good. Perhaps Andrei was behind it too. 1 Share this post Link to post
chkaufmann 17 Posted June 17, 2020 5 hours ago, Stefan Glienke said: If there is a bug with that please link to the QP entry and/or code to repro wrong behavior. The problem is to create a reproducable case. The order of initialization sections and class constructor is at least partial random. So things work fine but if you add a unit or change one of the uses in a project, the order is different. So I don't use class constructor anymore. Share this post Link to post
Stefan Glienke 2002 Posted June 17, 2020 2 hours ago, chkaufmann said: The problem is to create a reproducable case. The order of initialization sections and class constructor is at least partial random. So things work fine but if you add a unit or change one of the uses in a project, the order is different. So I don't use class constructor anymore. Sounds more like you have some serious issue with your globals Share this post Link to post
David Heffernan 2345 Posted June 17, 2020 5 hours ago, chkaufmann said: The order of initialization sections and class constructor is at least partial random. No. It is deterministic. Share this post Link to post