从clojure中的其他文件导入函数
Importing function from other file in clojure
如何在闭包中导入函数?假设我有两个文件:
test.clj
test2.clj
我想在 test
中使用 test2
中的函数。
我在 test
中尝试了以下方法,但这不起作用:
(namespace user :require test2)
如何在另一个文件中导入函数?
基本上想做 `from lib import f in python
您的命名空间语法有点不对。当我需要提醒时,我通常会参考 this cheat-sheet。
我认为以下是您要查找的语法。
;; In test.clj
(ns test
(:require [test2 :refer [some-symbol-to-import]]))
在文件 test.clj
中:
(ns test
(:require [test2 :as t2]))
(defn myfn [x]
(t2/somefn x)
(t2/otherfn x))
在上面的例子中,t2
是命名空间 test2
的别名。相反,如果您更喜欢从命名空间添加指定符号,请使用 :refer
:
(ns test
(:require [test2 :refer [somefn otherfn]]))
(defn myfn [x]
(somefn x)
(otherfn x))
要引用命名空间中的所有 public 符号,请使用 :refer :all
:
(ns test
(:require [test2 :refer :all]))
(defn myfn [x]
(somefn x)
(otherfn x))
如何在闭包中导入函数?假设我有两个文件:
test.clj
test2.clj
我想在 test
中使用 test2
中的函数。
我在 test
中尝试了以下方法,但这不起作用:
(namespace user :require test2)
如何在另一个文件中导入函数?
基本上想做 `from lib import f in python
您的命名空间语法有点不对。当我需要提醒时,我通常会参考 this cheat-sheet。
我认为以下是您要查找的语法。
;; In test.clj
(ns test
(:require [test2 :refer [some-symbol-to-import]]))
在文件 test.clj
中:
(ns test
(:require [test2 :as t2]))
(defn myfn [x]
(t2/somefn x)
(t2/otherfn x))
在上面的例子中,t2
是命名空间 test2
的别名。相反,如果您更喜欢从命名空间添加指定符号,请使用 :refer
:
(ns test
(:require [test2 :refer [somefn otherfn]]))
(defn myfn [x]
(somefn x)
(otherfn x))
要引用命名空间中的所有 public 符号,请使用 :refer :all
:
(ns test
(:require [test2 :refer :all]))
(defn myfn [x]
(somefn x)
(otherfn x))