Hi
First of all, let's make it straight - I am really bad programmer and there is no hope I will be better.
The problem is not concerning Nim per se, but having different tools in language its solutions may differ.
I have problem over and over with some circular dependences that arise among modules. This is annoying, time consuming, frustrating. By no means I claim it is language flow, but rather my. However it does not help. Let's also asume that we are not using methods and such kind of polymorphism.
Do you have also similar problems or it's just me? What is your approach to solve them?
I have found that there is some tool which can generate dependency graph (pity that it does not work when compilation have not finished successfuly just partially generating graph and allowing to see what's wrong).
As always any advice much appreciated.
I don't know your situation well, hard to guess.
in my experience, usually one object type need reference to other type, and that other type or it's descendant need to refers back to the first object type.
in that situation, the need to do circular import arise if those object definition were put into different module(s).
simple solution: put those interdependent objects(objects with circular reference) definition in one module
or rewrite your code with different approach that need no circular reference.
this solution sometimes feel inelegant, but the feeling will fade away....
Exactly. Sometimes loop involves like 6 modules which is not easy to track. Two possible solutions is to merge things, or split into smaller more independent pieces. If Nim had namespaces I could spread things in modules and use namespaces as logical units. Otherwise both above solutions are messy. By no means I argue here that Nim should have namespaces.
The pain is that when you have circular dependency you start moving things (together or split) just to fall into another circle, while still trying to keep it logical.
I am wondering if there is something like patterns or common sense at least, which would help me just keep structure neat, avoiding falling into such troubles. Funny, that when I finally got more familiar with language it is my biggest obstacle.
Having written that another idea came to my mind. I am not big fan of programming patterns, but since Nim in few areas allows different approach from Java style OOP, it would be interesting to see which patterns can be adopted, which are not needed and maybe even new ones can be introduced?
What do you think?
There are multiple workarounds but I found the RootRef hack easiest to explain:
# module ma
type
A = object
b: RootRef # a little lie goes a long way
# module B
import ma
type
B = ref object of RootRef
backRef: ref A
proc accessFieldB*(a: A): B = B(a.b)