【问题标题】:Importing function from other file in clojure从clojure中的其他文件导入函数
【发布时间】:2021-07-09 19:41:19
【问题描述】:

如何在闭包中导入函数?假设我有两个文件:

test.clj
test2.clj

我想在test中使用来自test2的函数。

我在test 中尝试了以下方法,但这不起作用:

(namespace user :require test2)

如何在另一个文件中导入函数?

基本上想做`from lib import f in python

【问题讨论】:

    标签: clojure


    【解决方案1】:

    您的命名空间语法有点偏离。当我需要提醒时,我通常会参考this cheat-sheet

    认为以下是您要查找的语法。

    ;; In test.clj
    (ns test
      (:require [test2 :refer [some-symbol-to-import]]))
    

    【讨论】:

      【解决方案2】:

      在文件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))
      

      要引用命名空间中的所有公共符号,请使用:refer :all

      (ns test
        (:require [test2 :refer :all]))
      
      (defn myfn [x]
        (somefn x)
        (otherfn x))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-19
        • 2018-10-19
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        相关资源
        最近更新 更多