Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic load dependency in Clojure

My project has a simple structure as following:

|- core.clj
|- dialogs.clj
|- dialogs/
   |- name_dialog.clj

name_dialog has a dependency from core, and core should require name_dialog.

So I have dependencies like this:

core.clj

(ns ddsl.core
  (:gen-class)
  (:require [clojure.xml :refer :all]
            [ddsl.dialogs :refer :all]))

dialogs.clj

(ns ddsl.dialogs
    (:require [ddsl.core :refer :all]))

(load "dialogs/name_dialog")

name_dialog.clj

(in-ns 'ddsl.dialogs)

When I try to run the program I'm getting the following error Cyclic load dependency: [ /ddsl/core ]->/ddsl/dialogs->[ /ddsl/core ]

Please let me know, how to restructure my project (i'm a novice in Clojure).

like image 704
lich Avatar asked Oct 07 '15 11:10

lich


1 Answers

The classic answer, not specifically Clojure related, may be to review the modules and their responsibilities.

(-> below stands for "depends on")

Given:

core -> dialogs -> core

Extract the part of the core module which is required by dialogs into a separate shared module:

shared (depends on "nothing")
core -> dialogs -> shared
core -> shared (possibly)

As for me, cyclic dependencies are an indicator of something wrong with the design. Even when the technical problem is resolved (with load-time sequence or compilation, etc.), cyclic dependencies are typically a sign of tight coupling and still are worth fixing.

like image 141
Timur Avatar answered Sep 20 '22 13:09

Timur