Is there a way to forward-declare interfaces to avoid module interdependencies?
schveiguy at yahoo.com
Mon Nov 15 07:52:38 PST 2010
On Sun, 14 Nov 2010 05:28:29 -0500, Per Ångström <d-news at autark.se> wrote:
> On 2010-11-11 17:21, Steven Schveighoffer wrote:
>> First, you can't forward-declare classes in one file that are defined in
>> another file, instead of importing. The reason is because in D, the
>> module is the namespace that the class is declared in.
>> So for instance, when you define IStudent in IStudent.d, it's full name
>> is IStudent.Istudent
>> if you did something like:
>> interface IStudent;
>> in ITeacher.d, then it's full name would be ITeacher.IStudent, not
>> IStudent.IStudent. So it's not the same.
> I get it now. So there is no way out for me: I have to import any module
> whose interfaces I use, even though I'm only referring to their names.
>> Second, you are only having fits about forward declarations because you
>> aren't used to it :)
> Actually, I would say it's the other way around: I am used to them
> (being able to refer to an unknown identifier as long as I don't use it)
> in C and C++ and find it lacking in D.
I said that wrong, I meant forward *references* not forward
*declrations*. That is, referring to things that aren't declared yet.
> > That being said, D has had many forward reference
>> bugs (I believe there are still a few active ones), so it doesn't always
>> *just work*. Plus there's the issue of circular imports causing code to
>> fail at runtime due to module construction dependencies.
>> But in any case, you shouldn't worry about it, just import away, and the
>> compiler can take it.
> But I still think cutting down on intermodule dependencies is a good
> thing, for ease of comprehension and unit testing.
> Fortunately, by separating interface and implementation, one can at
> least keep the implementation modules free from interdependencies. It's
> not perfect but it's not a complete mess either.
It is one way to keep dependencies down. And less dependencies means less
coupling, meaning importing one module doesn't make you import (and
include the code for) many other modules. The compiler isn't yet mature
enough to trim out some code that is never used.
More information about the Digitalmars-d-learn