Home Documentation Download Blog Forum Projects
Label: ♦english ♦miscellaneous

[569] How can I have a cyclic dependency in dao?

Comment
Hello,

I have the following kind of code:

ComponentManager.dao:
load Component;

class ComponentManager {
private
  var _components : map<string, map<int, Component>>
}

Component.dao:
load ComponentManager;

interface Component {
  sub observe(mgr : ComponentManager)
  sub act(mgr : ComponentManager)
}

You can see the cyclic dependency: ComponentManager holds a list of Components, while the Components should have a callback that gets the ComponentManager as parameter.
Unfortunately, this code won't compile (in World.dao, I do "load ComponentManager;"), because when dao gets to Component.dao, it doesn't know the type "ComponentManager" yet:
[[ERROR]] in file "/windows/Xchange/projects/component-system-prototype-fail1/CompSys/Component.dao":
At line 7 : Invalid parameter list for --- " observe ";
[[ERROR]] in file "/windows/Xchange/projects/component-system-prototype-fail1/CompSys/Component.dao":
At line 40 : Invalid interface definition --- " interface Component { routine observe ... ";
[[ERROR]] in file "/windows/Xchange/projects/component-system-prototype-fail1/CompSys/ComponentManager.dao":
At line 3 : loading failed;
[[ERROR]] in file "/windows/Xchange/projects/component-system-prototype-fail1/World.dao":
At line 3 : loading failed;
[[ERROR]] in file "/windows/Xchange/projects/component-system-prototype-fail1/main.dao":
At line 3 : loading failed;
My question is, how could I possibly solve that? And in a more general way, how to work around such a cyclic dependency in dao? Is there something like forward declaration in dao? Or maybe you have a suggestion to restructure my code?
Comments
I encountered similar situation too. You can just define additional interface for ComponentManager in Component.dao ; use it as parameter type for Component 's methods, and then bind that interface to the actual class in ComponentManager.dao . Then you don't need to load ComponentManager.dao in Component.dao , and cross-dependency is eliminated :) However, the interface for ComponentManager will still be unable to explicitly refer to the interface Component (untyped arguments are needed for that instead).
is now supported for both class and interface:)
Wow cool, thanks a bunch!
Edit: I see in the example files, that now you declared the routine inside the class but defined it outside the class (C++-style), was that supported since ever, or did you just add that?
Define methods outside of class body has been supported for quite sometime:)

Change picture:

Choose file:

1234 5
67891011 12
131415161718 19
202122232425 26
2728293031

fu: A little bit game development in Dao! Thanks to ClangDao, it has become very easy to create bindings for C/ C++ libraries. The latest one i ... (May.14,07:08)

dao: Dao 1.2 Beta1 is released! After a very long time of development, the first beta release for Dao 1.2 is finally available ( http ... (May.06,23:37)

fu: ... Just to mention: a couple of demos (including the 2000 line one) has been successfully ported to IPho ... (May.19,02:43)

fu: ... Yes, it is getting mature, and more libraries and modules are coming out, hehe:) For GameKit, unfor ... (May.19,02:38)

Pompei2: ... This is cool news and really shows that ClangDao is getting mature, thumbs up. Too bad for this litt ... (May.18,09:17)

fu: ... Not completely, but mostly. New revisions will be regularly pushed to the repository on google code ( ... (May.08,22:38)

Pompei2: ... If I understand it correctly, you want to completely switch? If so then: (May.08,08:46)

This site is powered by Dao
Copyright (C) 2009-2012, daovm.net.
Webmaster: admin@daovm.net