|
4723 | 4723 | Optional keys:
|
4724 | 4724 |
|
4725 | 4725 | - ``:as ns-alias`` a symbol which will alias the Namespace when required (if given)
|
| 4726 | + - ``:as-alias ns-alias`` a symbol which will alias the Namespace when required; if |
| 4727 | + the namespace does not exist, it will be created as an empty namespace; if it does |
| 4728 | + exist, it will not be loaded |
4726 | 4729 | - ``:refer [sym1, sym2]`` a sequence of symbols naming Vars to refer
|
4727 | 4730 | - ``:refer :all`` if every Var should be referred
|
4728 | 4731 | - ``:only [sym1, sym2]`` a sequence of symbols naming Vars to refer
|
|
4761 | 4764 | ;; rewrote the namespace on require.
|
4762 | 4765 | (when-let [original-ns (:original-namespace libspec)]
|
4763 | 4766 | (.add-alias requiring-ns (the-ns required-ns-sym) original-ns))
|
| 4767 | + ;; If an `:as-alias` is requested, apply that as well. |
| 4768 | + (when-let [as-alias (:as-alias libspec)] |
| 4769 | + (.add-alias requiring-ns (the-ns required-ns-sym) as-alias)) |
4764 | 4770 | ;; Reset the namespace to the requiring namespace, since it was likely changed
|
4765 | 4771 | ;; during the require process
|
4766 | 4772 | (set! *ns* requiring-ns)))
|
|
4812 | 4818 | Vector libspec arguments must be one of:
|
4813 | 4819 |
|
4814 | 4820 | - ``:as name`` which will alias the imported namespace to the symbol name
|
| 4821 | + - ``:as-alias name`` which will alias the namespace to the symbol name but not |
| 4822 | + require the namespace, which can be useful for namespaces used primarily for |
| 4823 | + keywords; the namespace need not exist at all; can be combined with ``:as`` |
4815 | 4824 | - ``:refer [& syms]`` which will refer syms in the local namespace directly
|
4816 | 4825 | - ``:refer :all`` which will refer all symbols from the namespace directly
|
4817 | 4826 |
|
|
4820 | 4829 | [& args]
|
4821 | 4830 | (let [current-ns *ns*]
|
4822 | 4831 | (doseq [libspec (map require-libspec args)]
|
4823 |
| - (require-lib current-ns libspec) |
4824 |
| - |
4825 |
| - ;; Add refers |
4826 |
| - (let [new-ns (the-ns (:namespace libspec)) |
4827 |
| - refer-opt (:refer libspec)] |
4828 |
| - (cond |
4829 |
| - (= :all refer-opt) |
4830 |
| - (.refer-all current-ns new-ns) |
| 4832 | + (if (and (:as-alias libspec) (not (:as libspec))) |
| 4833 | + (let [alias-target (or (find-ns (:namespace libspec)) |
| 4834 | + (create-ns (:namespace libspec)))] |
| 4835 | + (.add-alias current-ns alias-target (:as-alias libspec))) |
| 4836 | + (do |
| 4837 | + (require-lib current-ns libspec) |
| 4838 | + |
| 4839 | + ;; Add refers |
| 4840 | + (let [new-ns (the-ns (:namespace libspec)) |
| 4841 | + refer-opt (:refer libspec)] |
| 4842 | + (cond |
| 4843 | + (= :all refer-opt) |
| 4844 | + (.refer-all current-ns new-ns) |
4831 | 4845 |
|
4832 |
| - (seq refer-opt) |
4833 |
| - (let [new-ns-interns (ns-interns new-ns)] |
4834 |
| - (doseq [var-sym refer-opt] |
4835 |
| - (let [var (get new-ns-interns var-sym)] |
4836 |
| - (.add-refer current-ns var-sym var)))) |
| 4846 | + (seq refer-opt) |
| 4847 | + (let [new-ns-interns (ns-interns new-ns)] |
| 4848 | + (doseq [var-sym refer-opt] |
| 4849 | + (let [var (get new-ns-interns var-sym)] |
| 4850 | + (.add-refer current-ns var-sym var)))) |
4837 | 4851 |
|
4838 |
| - :else nil))) |
| 4852 | + :else nil))))) |
4839 | 4853 | nil))
|
4840 | 4854 |
|
4841 | 4855 | (defn refer
|
|
4926 | 4940 | [basilisp.string :as str])
|
4927 | 4941 | (:use
|
4928 | 4942 | [basilisp.set :only [intersection]])
|
4929 |
| - (:import inspect))" |
| 4943 | + (:import inspect)) |
| 4944 | + |
| 4945 | + Flags and contents of each of the various sections are detailed further at each of |
| 4946 | + their respective function definitions. |
| 4947 | + |
| 4948 | + - ``:refer-basilisp`` (``:refer-clojure`` is also accepted) controls how names from |
| 4949 | + :lpy:ns:`basilisp.core` are exposed in the namespace. Refer to :lpy:fn:`refer` for |
| 4950 | + usage. |
| 4951 | + - ``:require`` imports other Basilisp namespaces. Refer to :lpy:fn:`require` for |
| 4952 | + usage. |
| 4953 | + - ``:use`` is a variant of ``:require``, although ``:require`` is preferred in most |
| 4954 | + cases. See :lpy:fn:`use` for usage. |
| 4955 | + - ``:import`` imports Python modules and packages. Refer to :lpy:fn:`import` for |
| 4956 | + usage. |
| 4957 | + |
| 4958 | + Use of the ``ns`` macro to control requires and imports is recommended in all cases. |
| 4959 | + The various functions that ``ns`` delegates to and whose documentation are referred |
| 4960 | + to above are generally useful for interactive usage at the REPL but not appropriate |
| 4961 | + for usage in a larger application." |
4930 | 4962 | [name & opts]
|
4931 | 4963 | (when-not (and (symbol? name) (nil? (namespace name)))
|
4932 | 4964 | (throw (ex-info "Namespace name must be a non-namespaced symbol"
|
|
4966 | 4998 | ~@imports)))
|
4967 | 4999 |
|
4968 | 5000 | (defn requiring-resolve
|
4969 |
| - "Resolve the namespaced symbol ``sym`` as by ``resolve``\\. If resolution fails, |
4970 |
| - attempts to require ``sym`` 's namespace (as by ``require``\\) before resolving |
| 5001 | + "Resolve the namespaced symbol ``sym`` as by :lpy:fn:`resolve`\\. If resolution fails, |
| 5002 | + attempts to require ``sym`` 's namespace (as by :lpy:fn:`require`\\) before resolving |
4971 | 5003 | again."
|
4972 | 5004 | [sym]
|
4973 | 5005 | (if (qualified-symbol? sym)
|
|
0 commit comments