From f5fcdc7b49a5d4384a6a996469f4d9ed5dfccbb8 Mon Sep 17 00:00:00 2001 From: Andre Ambrosio Boechat Date: Fri, 15 Jan 2016 15:47:47 +0100 Subject: [PATCH] with-collection macro: type hints improvement From all the resources I found about type hinting in macros, it doesn't work like it does for normal functions. I got reflection warnings for a code like this: ```clj (let [conn (mg/connect) db (delay (mg/get-db conn "monger-test"))] (with-collection @db "something" (find {}))) ``` The type hint for `db` didn't work. Actually the type hint that works comes from the function `core/get-db` and we see this when we remove `delay` from the code above. As we could expect, the function `deref` doesn't propagate the type hint. I did similar tests with the collection name and no reflection warning was raised for any case. In addition, it looked weird for me to have a type hint like `^String` and then a checking like `string?` later. Some references: * http://stackoverflow.com/questions/11919602/generating-clojure-code-with-type-hints * Clojure High Performance Programming, page 44. --- src/clojure/monger/query.clj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/clojure/monger/query.clj b/src/clojure/monger/query.clj index 2d93313..e8caa65 100644 --- a/src/clojure/monger/query.clj +++ b/src/clojure/monger/query.clj @@ -167,10 +167,11 @@ (merge m { :limit per-page :skip (monger.internal.pagination/offset-for page per-page) })) (defmacro with-collection - [^DB db ^String coll & body] + [db coll & body] `(let [coll# ~coll + ^DB db# ~db db-coll# (if (string? coll#) - (.getCollection ~db ^String coll#) + (.getCollection db# coll#) coll#) query# (-> (empty-query db-coll#) ~@body)] (exec query#)))