0
Follow
2
View

Algorithm to resolve circular dependencies?

tuoluzhe 注册会员
2023-01-25 02:49

The answer to your question depends on what you mean by "resolve".

Consider the following situation:

You have three classes, A, B and C, which end on each other in the circular way.

After the first pass (loading), you have access to all the information about each class, that is written in the source files.

Now, let's say you want to fully "resolve" class A. Pretend you don't care about other classes for now.

Given just the information from files, is it possible to perform the desired "resolve" just for the A?


If the answer is , then the answer to your question is no as well.

If the answer is , then it means that two passes are sufficient to fully resolve every class. You can do this either recursively or iteratively, and you'd probably want to cache fully resolved classes to avoid resolving them multiple times, although, it's not necessary for correctness (it's purely a performance optimization).