Skip to content

Commit

Permalink
Type hints in function test
Browse files Browse the repository at this point in the history
The function namespace requires type hints in many cases to avoid
reflection which fails in Java 9+.
  • Loading branch information
ajoberstar committed May 6, 2018
1 parent 3169e12 commit f39c7ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The `ike.cljj.function` namespace includes three main helpers for this:
* `sam` - creating an anonymous SAM impl, as it were a Clojure function
* `defsam` - defining a named SAM impl, as if it were a Clojure function

**WARNING:** You may need type hints to avoid `IllegalAccessError` in Java 9+.

```clojure
(defsam my-sam
java.util.function.Predicate
Expand Down
21 changes: 10 additions & 11 deletions src/test/clojure/ike/cljj/function_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@
(:import (java.util.function Supplier Consumer Function BiFunction)))

(deftest supplier
(let [f (sam* Supplier (fn [] "supplied"))]
(let [^Supplier f (sam* Supplier (fn [] "supplied"))]
(is (= "supplied" (.get f)))))

(deftest consumer
(let [p (promise)
f (sam* Consumer (fn [x] (deliver p x)))]
^Consumer f (sam* Consumer (fn [x] (deliver p x)))]
(is (nil? (.accept f 123)))
(is (= 123 @p))))

(deftest function
(let [f (sam* Function (fn [x] (* x 2)))]
(let [^Function f (sam* Function (fn [x] (* x 2)))]
(is (= 4 (.apply f 2)))))

(deftest bifunction
(let [f (sam* BiFunction (fn [x y] (* x y)))]
(let [^BiFunction f (sam* BiFunction (fn [x y] (* x y)))]
(is (= 6 (.apply f 2 3)))))


(deftest sam-macro-supplier
(let [f (sam Supplier [] "supplied")]
(let [^Supplier f (sam Supplier [] "supplied")]
(is (= "supplied" (.get f)))))

(deftest defsam-macro-supplier
(defsam f Supplier [] "supplied")
(is (= "supplied" (.get f))))
(defsam f1 Supplier [] "supplied")
(is (= "supplied" (.get ^Supplier f1))))

(deftest defsam-docstring-macro-supplier
(defsam f "It worked!" Supplier [] "supplied")
(is (= "supplied" (.get f)))
(is (= "It worked!" (:doc (meta #'f)))))
(defsam f2 "It worked!" Supplier [] "supplied")
(is (= "supplied" (.get ^Supplier f2)))
(is (= "It worked!" (:doc (meta #'f2)))))

0 comments on commit f39c7ea

Please sign in to comment.