public
Created

A Thorny Issue In Clojure Java Interop

  • Download Gist
thorny.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
(ns a-thorny-issue.core
(require [clojure [reflect :as r] [pprint :as pp]]
[clojure.java [javadoc :as jd]])
(import [java.util.concurrent Executors Future ExecutorService]))
 
(comment (pp/print-table [:name :parameter-types :return-type]
(sort-by :name (:members
(r/reflect java.util.concurrent.ExecutorService))))
 
(jd/javadoc java.util.concurrent.ExecutorService))
 
(def an-executor (Executors/newSingleThreadExecutor))
 
(defn submit-callable [c ex]
(.submit ex c))
 
(defn some-hard-work []
34)
 
(instance? Callable some-hard-work)
(instance? Runnable some-hard-work)
 
(instance? Future (submit-callable some-hard-work an-executor))
 
(.get (submit-callable some-hard-work an-executor))
 
(defn submit-callable-reify [c ex]
(.submit ex (reify Callable (call [_] (c)))))
 
(instance? Future (submit-callable-reify some-hard-work an-executor))
 
(.get (submit-callable-reify some-hard-work an-executor))
 
(defn submit-callable-hint [^Callable c ex]
(.submit ex c))
 
(defn submit-callable-hint2 [c ex]
(.submit ex ^Callable c))
 
(defn submit-callable-cast [c ex]
(.submit ex (cast Callable c)))
 
(defn submit-callable-hint3 [c ex]
(let [^Callable the-fn c]
(.submit ex the-fn)))
 
(defn submit-callable-hint4 [c]
(let [ex (Executors/newSingleThreadExecutor)
^Callable the-fn c]
(.submit ex the-fn)))
 
(defn submit-callable-hint5 [c ex]
(let [the-ex ex
^Callable the-fn c]
(.submit ex the-fn)))
 
;; Success!!
(defn submit-callable-hint6 [^Callable c ^ExecutorService ex]
(.submit ex c))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.