Skip to content

Commit f7e03ca

Browse files
authored
Add the-ns and create-ns functions (#229)
* Add the-ns and create-ns functions * Update the docstring
1 parent 22bac3d commit f7e03ca

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/basilisp/core/__init__.lpy

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,26 @@
10651065
;; Namespace Utilities ;;
10661066
;;;;;;;;;;;;;;;;;;;;;;;;;
10671067

1068-
(defn ^:private get-namespace
1069-
"Get the Namespace named by ns-sym."
1068+
(defn the-ns
1069+
"If v is a symbol, return the Namespace named by that symbol if it
1070+
exists. If v is a Namespace, return it. Otherwise, throw an exception."
1071+
[v]
1072+
(cond
1073+
(symbol? v)
1074+
(.get basilisp.lang.runtime/Namespace v)
1075+
1076+
(instance? basilisp.lang.runtime/Namespace v)
1077+
v
1078+
1079+
:else
1080+
(throw
1081+
(ex-info "Expected a Symbol or a Namespace"
1082+
{:value v
1083+
:type (builtins/type v)}))))
1084+
1085+
(defn create-ns
1086+
"Create a Namespace with the name ns-sym or return the existing one
1087+
if it already exists."
10701088
[ns-sym]
10711089
(.get-or-create basilisp.lang.runtime/Namespace ns-sym))
10721090

@@ -1152,7 +1170,7 @@
11521170
- :rename {sym1 new-sym1}, to rename all the specified symbols to the given new name"
11531171
[ns-sym & filters]
11541172
(let [current-ns *ns*
1155-
ns (get-namespace ns-sym)
1173+
ns (the-ns ns-sym)
11561174
filter-map (apply hash-map filters)]
11571175
(if-not (seq filters)
11581176
(.refer-all current-ns ns)
@@ -1205,7 +1223,7 @@
12051223
(let [ns-sym (first v)
12061224
opts (apply hash-map (rest v))]
12071225
(importlib/import-module (name ns-sym))
1208-
(let [new-ns (get-namespace ns-sym)]
1226+
(let [new-ns (the-ns ns-sym)]
12091227
;; Add the namespace alias
12101228
(.add-alias current-ns (or (:as opts) ns-sym) new-ns)
12111229

0 commit comments

Comments
 (0)