【问题标题】:Cyclic load dependency in ClojureClojure 中的循环负载依赖关系
【发布时间】:2016-01-04 15:10:32
【问题描述】:

我的项目结构简单如下:

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

name_dialog 具有来自 core 的依赖关系,而 core 应该需要 name_dialog

所以我有这样的依赖:

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)

当我尝试运行程序时,出现以下错误 Cyclic load dependency: [ /ddsl/core ]->/ddsl/dialogs->[ /ddsl/core ]

请告诉我,如何重组我的项目(我是 Clojure 的新手)。

【问题讨论】:

  • 为什么core 需要其他命名空间?
  • core 从 clojure "template" 生成 xml,并具有接收模板名称作为参数的 -main 函数,例如"name-dialog" 并从中生成 xml
  • (defn state [s & xs] (hash-map :tag :state :attrs {:name s} :content (if xs (vec xs) nil)))
  • 听起来core 中依赖于其他命名空间的函数应该移到这些命名空间中。 core 命名空间不需要依赖其他模块。
  • 谢谢,我会试着移动它们,这很合乎逻辑。但是在core 我有接收对话框名称的函数,例如dialog_name,并生成 xml,所以无论如何都应该作为模块加载? (defn -main [dialog] (emit (eval (symbol dialog)))))

标签: clojure cyclic-dependency


【解决方案1】:

与 Clojure 无关的经典答案可能是查看模块及其职责。

(以下-> 代表“取决于”)

给定:

core -> dialogs -> core

dialogs所需的core模块部分提取到单独的共享模块中:

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

对我来说,循环依赖是设计有问题的一个指标。即使解决了技术问题(通过加载时间序列或编译等),循环依赖通常是紧密耦合的标志,仍然值得修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2021-12-21
    • 2022-01-09
    相关资源
    最近更新 更多