Cyclic dependencies vs static constructors
Jean-Louis Leroy
jl at leroy.nyc
Tue Dec 19 17:43:46 UTC 2017
This has come up in a private discussion with Luís Marques.
Consider:
import std.stdio;
import openmethods;
mixin(registerMethods);
class Company
{
Employee[] employees;
}
class Startup : Company
{
}
class Role
{
Role[] companies;
}
class Founder : Role
{
}
class Employee : Role
{
}
void main()
{
// blah blah blah
}
As the program grows bigger, I want to split it into several
modules, e.g. one per class or one per hierarchy. Now I have
cyclic dependencies between modules because role.d must 'import
company' and 'company.d' must 'import role'.
It seems that at this point I have forgone a language feature:
static constructors. They may be needed if I want to implement
e.g. a global registry in Role and Company that tracks all the
instances of the classes in their respective hierarchy.
At this point the only workaround I see is to add base interfaces
under Role and Company and establish the bi-directional
relationship in terms of the said interfaces.
...or I can throw in that flag that allows cyclic deps in
presence of static constructors. Eventually I may run into
trouble.
Have I overlooked something?
More information about the Digitalmars-d
mailing list