Is there a way to forward-declare interfaces to avoid module interdependencies?

Per Ångström d-news at autark.se
Sun Nov 14 02:28:29 PST 2010


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.

 > 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.

Cheers,
-- 
Per Å.


More information about the Digitalmars-d-learn mailing list