Skip to content

Commit 93809a4

Browse files
committed
Replace declare+ with defn #487
1 parent 4562d2c commit 93809a4

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

src/datascript/db.cljc

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[me.tonsky.persistent-sorted-set :as set]
1010
[me.tonsky.persistent-sorted-set.arrays :as arrays])
1111
#?(:clj (:import clojure.lang.IFn$OOL))
12-
#?(:cljs (:require-macros [datascript.db :refer [case-tree combine-cmp declare+ defn+ defcomp defrecord-updatable int-compare validate-attr validate-val]]))
12+
#?(:cljs (:require-macros [datascript.db :refer [case-tree combine-cmp defn+ defcomp defrecord-updatable int-compare validate-attr validate-val]]))
1313
(:refer-clojure :exclude [seqable? #?(:clj update)]))
1414

1515
#?(:clj (set! *warn-on-reflection* true))
@@ -80,24 +80,14 @@
8080
'number (assoc meta :tag clojure.core$long)
8181
meta))))
8282

83-
#?(:clj
84-
(defmacro declare+
85-
"Same idea as `declare`, but allows to declare type hints and arglists.
86-
This allows CLJS to generate more efficient code when calling this fn
87-
before it’s declared"
88-
[name & arglists]
89-
(let [name' (vary-meta name patch-tag (cljs-env? &env))
90-
bodies (map #(list % `(throw (ex-info (str "Not implemented: (" ~name (clojure.string/join " " ~%)) {}))) arglists)]
91-
`(defn ~name' ~@bodies))))
92-
9383
#?(:clj
9484
(defmacro defn+
95-
"Version of `defn` that works with `declare+`. CLJS really don’t like
96-
:declared metadata on vars (generates less efficient code), but it
97-
needs it to skip warnings. So we redefine first with ^:declared
98-
and empty implementation, and then immediately redefine again without ^:declared.
99-
This way both `declare+`-d and `defn+`-d versions have no ^:declared meta,
100-
thus allowing CLJS to generate direct invocations and see type hints."
85+
"CLJS really don’t like :declared metadata on vars (generates less
86+
efficient code), but it needs it to skip warnings. So we redefine
87+
first with ^:declared and empty implementation, and then immediately
88+
redefine again without ^:declared. This way both `declare` and `defn+`
89+
versions have no ^:declared meta, thus allowing CLJS to generate direct
90+
invocations and see type hints."
10191
[name & rest]
10292
(let [name' (vary-meta name patch-tag (cljs-env? &env))
10393
arglists (if (vector? (first rest))
@@ -163,17 +153,23 @@
163153

164154
;; ----------------------------------------------------------------------------
165155

166-
#?(:cljs (declare+ ^number hash-datom [d]) :clj (declare hash-datom))
156+
#?(:clj (declare hash-datom)
157+
:cljs (defn ^number hash-datom [d]))
167158

168-
#?(:cljs (declare+ ^boolean equiv-datom [d o]) :clj (declare equiv-datom))
159+
#?(:clj (declare equiv-datom)
160+
:cljs (defn ^boolean equiv-datom [d o]))
169161

170-
#?(:cljs (declare+ seq-datom [d]) :clj (declare seq-datom))
162+
#?(:clj (declare seq-datom)
163+
:cljs (defn seq-datom [d]))
171164

172-
#?(:cljs (declare+ nth-datom [d i] [d i not-found]) :clj (declare nth-datom))
165+
#?(:clj (declare nth-datom)
166+
:cljs (defn nth-datom ([d i]) ([d i not-found])))
173167

174-
#?(:cljs (declare+ assoc-datom [d k v]) :clj (declare assoc-datom))
168+
#?(:clj (declare assoc-datom)
169+
:cljs (defn assoc-datom [d k v]))
175170

176-
#?(:cljs (declare+ val-at-datom [d k not-found]) :clj (declare val-at-datom))
171+
#?(:clj (declare val-at-datom)
172+
:cljs (defn val-at-datom [d k not-found]))
177173

178174
(defprotocol IDatom
179175
(datom-tx [this])
@@ -409,7 +405,8 @@
409405
#?(:clj (. clojure.lang.Util (hasheq x))
410406
:cljs (hash x)))
411407

412-
#?(:cljs (declare+ ^number value-compare [x y]) :clj (declare value-compare))
408+
#?(:clj (declare value-compare)
409+
:cljs (defn ^number value-compare [x y]))
413410

414411
(defn- seq-compare [xs ys]
415412
(let [cx (count xs)
@@ -584,22 +581,28 @@
584581

585582
;; ----------------------------------------------------------------------------
586583

587-
#?(:cljs (declare+ ^number hash-db [db]) :clj (declare hash-db))
584+
#?(:clj (declare hash-db)
585+
:cljs (defn ^number hash-db [db]))
588586

589-
#?(:cljs (declare+ ^number hash-fdb [db]) :clj (declare hash-fdb))
587+
#?(:clj (declare hash-fdb)
588+
:cljs (defn ^number hash-fdb [db]))
590589

591-
#?(:cljs (declare+ ^boolean equiv-db [db other]) :clj (declare equiv-db))
590+
#?(:clj (declare equiv-db)
591+
:cljs (defn ^boolean equiv-db [db other]))
592592

593-
#?(:cljs (declare+ restore-db [keys]) :clj (declare restore-db))
593+
#?(:clj (declare restore-db)
594+
:cljs (defn restore-db [keys]))
594595

595-
#?(:cljs (declare+ ^boolean indexing? [db attr]) :clj (declare indexing?))
596+
#?(:clj (declare indexing?)
597+
:cljs (defn ^boolean indexing? [db attr]))
596598

597-
#?(:cljs
598-
(declare+ pr-db [db w opts]))
599+
#?(:cljs (defn pr-db [db w opts]))
599600

600-
#?(:cljs (declare+ resolve-datom [db e a v t default-e default-tx]) :clj (declare resolve-datom))
601+
#?(:clj (declare resolve-datom)
602+
:cljs (defn resolve-datom [db e a v t default-e default-tx]))
601603

602-
#?(:cljs (declare+ components->pattern [db index c0 c1 c2 c3 default-e default-tx]) :clj (declare components->pattern))
604+
#?(:clj (declare components->pattern)
605+
:cljs (defn components->pattern [db index c0 c1 c2 c3 default-e default-tx]))
603606

604607
;;;;;;;;;; Fast validation
605608

@@ -1122,9 +1125,11 @@
11221125

11231126
;; ----------------------------------------------------------------------------
11241127

1125-
#?(:cljs (declare+ ^number entid-strict [db eid]) :clj (declare entid-strict))
1128+
#?(:clj (declare entid-strict)
1129+
:cljs (defn ^number entid-strict [db eid]))
11261130

1127-
#?(:cljs (declare+ ^boolean ref? [db attr]) :clj (declare ref?))
1131+
#?(:clj (declare ref?)
1132+
:cljs (defn ^boolean ref? [db attr]))
11281133

11291134
(defn+ resolve-datom [db e a v t default-e default-tx]
11301135
(when (some? a)
@@ -1619,7 +1624,8 @@
16191624
(filter (fn [^Datom d] (component? db (.-a d))))
16201625
(map (fn [^Datom d] [:db.fn/retractEntity (.-v d)]))) datoms))
16211626

1622-
#?(:cljs (declare+ transact-tx-data-impl [initial-report initial-es]) :clj (declare transact-tx-data-impl))
1627+
#?(:clj (declare transact-tx-data-impl)
1628+
:cljs (defn transact-tx-data-impl [initial-report initial-es]))
16231629

16241630
(defn- retry-with-tempid [initial-report report es tempid upserted-eid]
16251631
(if-some [eid (get (::upserted-tempids initial-report) tempid)]

0 commit comments

Comments
 (0)