Preventing Circular imports

BlitzMax Forums/BlitzMax Programming/Preventing Circular imports

MrHanson(Posted 2005) [#1]
As a C++ programmer, I'm having trouble deciding on how to break down my files into multiple sources.

Say I have 3 files:

test.bmx is listed as:

Import "Ship.bmx"
Import "Alien.bmx"
Local myship:Ship=New Ship
Local enemy:Alien=New Alien
myship.enemy=enemy
enemy.enemy=myship

Alien.bmx is listed as:

Import "Ship.bmx"
Type Alien
Field enemy:Ship
End Type

and Ship.bmx is listed as:

Import "Alien.bmx"
Type Ship
Field enemy:Alien
End Type

When I compile the main program "test.bmx" I get a Duplicate Identifier 'Ship' compiler error. In C++, usually a #ifndef,#define and a #endif directive prevents this sort of thing. Is there something similar in BMax? If not, could there be a possibilty in the future? I guess I could use "include" instead, but "import" is much more desirable.


MrHanson(Posted 2005) [#2]
Oh, nevermind. Should have done a search on "cyclic imports" in the forums and I see people are having the same issues on how to organize their source code. Being able to cross-reference unrelated classes is highly desirable and hopefully will get addressed in future releases. For now I guess I'll just have to use "include".


FlameDuck(Posted 2005) [#3]
Being able to cross-reference unrelated classes is highly desirable
Well that's certainly debatable. Most people I know would argue that a high coupling is very much undesirable as it needlessly increases program complexity.


Ferminho(Posted 2005) [#4]
I'm quite used to C++'s prep. directives, that's why it's in my Bmax wishlist. Would be really good for some things (being this one of them)


Warpy(Posted 2005) [#5]
Sort of the same topic - I'd like to be able to set it so when I'm looking at one of the includes for my program, Ctrl+B builds the main file, not the one I'm looking at. Either that, or make F6 work like it did in B3D.


MrHanson(Posted 2005) [#6]
"Well that's certainly debatable. Most people I know would argue that a high coupling is very much undesirable as it needlessly increases program complexity. "

I disagree, having to organize your source code around a limitation that could probably be easily remedied forces you to write your programs at a highler level of abstraction, therefore making it more difficult to read and write.


Muttley(Posted 2005) [#7]
After causing myself all kinds of grief with circular imports, I made an executive decision to only import modules (and if need be turn some of my stuff into proper modules) and just include the rest and be damned. ;)

Muttley