【问题标题】:Injecting namespaces in functions in clojure在 clojure 的函数中注入命名空间
【发布时间】:2021-07-11 12:09:01
【问题描述】:

是否可以在 Clojure 中的函数中注入命名空间?

我希望我的 i/o 在程序之外并且只注入它。我面临的问题是,我试图提供一个命名空间并调用它,但我收到了错误消息:

No name namespace: my
(ns mymain
  (:require [myio]))

...

(defn my-test [my]
  (my/showworld))

;(play_game)
(my-test myio)

【问题讨论】:

  • 我猜你需要ns-resolve

标签: clojure


【解决方案1】:

通常的逻辑传递方式是通过函数而不是命名空间别名:

(defn my-test [show-world]
  (show-world))

(require 'myio)
(my-test myio/show-world)

【讨论】:

  • 如何为命名空间创建模拟对象?
  • 你通常不模拟命名空间,但你可以模拟函数,例如使用 with-redefs。
猜你喜欢
  • 2013-03-12
  • 2023-03-05
  • 2011-08-31
  • 2015-05-25
  • 2011-11-07
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
相关资源
最近更新 更多