簡體   English   中英

Clojure - 需要在 repl 中聲明的命名空間

[英]Clojure - require a namespace that was declared in the repl

假設我想在 REPL 中執行以下操作:

(ns a1)

(defn adder [a b]
  (+ a b))

(ns b1
  (:require [a1 :as a]))

(println (a/adder 1 2))

如果代碼是根據聲明的命名空間在文件中定義的,那么這可以正常工作。 但是,如果我們在 repl 中這樣做 - 那么我們會在這一行收到錯誤:

(ns b1
  (require [a1 :as a]))

錯誤是:

FileNotFoundException Could not locate a1__init.class or a1.clj on classpath:   
clojure.lang.RT.load (RT.java:432)

現在我知道我可以明確地將命名空間的全名放在:

(println (a1/adder 1 2))

但我想縮寫我的命名空間。 (如果我的命名空間長度為 20 個字符或更多)。

我要更改哪些內容以確保我可以以縮寫方式引用 repl 上的不同命名空間?

require在內部使用函數alias來執行您想要的操作:

(ns b1)
(alias 'a 'a1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM