leonoel / cloroutine

Coroutine support for clojure
Eclipse Public License 2.0
228 stars 10 forks source link

reflection errors for this code example: #18

Open zcaudate opened 3 years ago

zcaudate commented 3 years ago

Given the code example, this gives a warning:

(require '[cloroutine.core :refer [cr]])
(import '(java.util.concurrent CompletableFuture))

(def ^:dynamic *fiber*)
(def ^:dynamic *value*)
(def ^:dynamic *error*)
(defn await [cf] (.whenComplete ^CompletableFuture cf *fiber*))
(defn thunk [] (if-some [e *error*] (throw e) *value*))

(defmacro async [& body]
  `(let [cf# (CompletableFuture.)
         cr# (cr {await thunk}
               (try (.complete cf# (do ~@body))
                    (catch Throwable e#
                      (.completeExceptionally cf# e#))))]
     (binding [*fiber* (reify java.util.function.BiConsumer
                         (accept [f# v# e#]
                           (binding [*fiber* f#
                                     *value* v#
                                     *error* e#]
                             (cr#))))]
       (cr#)) cf#))

;;; turn on reflections
(set! *warn-on-reflection* true)

;;; example
(async (await (async 1))
;;;       |
;;;       ^ reflection warning