Created
December 4, 2012 20:44
-
-
Save bloat/4208492 to your computer and use it in GitHub Desktop.
A Thorny Issue In Clojure Java Interop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment