I’ve been working on a labelling system and I have the following two modules, which result in a deadlock. Now I think I understand why, as they reference each other; I’m just curious what you would do different to make it work.
I’m currently thinking that a Label shouldn’t be aware of a List, but not entirely convinced about that.
NB: I’ve refrained from calling it a taxonomy system, as I only plan on Lists having one level of labels. Otherwise I’d simply have a Term which could contain Terms. I’m avoiding that for now.
defmodule List do
def create(name) do
%List{name: name}
end
def add_label(label = %Label{) do
[label] ++ labels
end
end
defmodule Label do
def create(list = %List{}, name) do
...
end
end
Do you really have a toplevel module called List? There’s already one in :elixir, you should rename that one.
Also, to be sure if it really is tied to the circular dependency or not, you can try to replace the pattern-matches on the struct by a generic catch-all match.