Support reifying clojure.lang.IFn and clojure.lang.ILookup (#740)

This commit is contained in:
Michiel Borkent 2021-02-28 18:23:45 +01:00 committed by GitHub
parent f59b0efab4
commit e64dbc7691
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 14 deletions

2
sci

@ -1 +1 @@
Subproject commit 96a0c77d75066746b91ab242dc144d04b10b399b
Subproject commit d174e28fcb05a53868404c7cd0efc43093b66442

View file

@ -18,23 +18,53 @@
(mapcat
(fn [[clazz methods]]
(cons clazz
(map
(fn [[meth args]]
(list meth args
(list*
(list 'get-in 'methods
[(list 'quote clazz) (list 'quote meth)])
args)))
methods)))
(mapcat
(fn [[meth arities]]
(map
(fn [arity]
(list meth arity
(list*
(list 'get-in 'methods
[(list 'quote clazz) (list 'quote meth)])
arity)))
arities))
methods)))
classes)))))
{}
subsets)))
#_(prn (macroexpand '(gen-reify-combos
{java.io.FileFilter {accept [[this f]]}})))
#_:clj-kondo/ignore
(def reify-opts
(gen-reify-combos
{java.nio.file.FileVisitor {preVisitDirectory [this p attrs]
postVisitDirectory [this p attrs]
visitFile [this p attrs]}
java.io.FileFilter {accept [this f]}
java.io.FilenameFilter {accept [this f s]}}))
{java.nio.file.FileVisitor {preVisitDirectory [[this p attrs]]
postVisitDirectory [[this p attrs]]
visitFile [[this p attrs]]}
java.io.FileFilter {accept [[this f]]}
java.io.FilenameFilter {accept [[this f s]]}
clojure.lang.ILookup {valAt [[this k] [this k default]]}
clojure.lang.IFn {applyTo [[this arglist]]
invoke [[this]
[this a1]
[this a1 a2]
[this a1 a2 a3]
[this a1 a2 a3 a4]
[this a1 a2 a3 a4 a5]
[this a1 a2 a3 a4 a5 a6]
[this a1 a2 a3 a4 a5 a6 a7]
[this a1 a2 a3 a4 a5 a6 a7 a8]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20]
[this a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 varargs]]}}))

View file

@ -576,6 +576,36 @@
(when test-utils/native?
(is (bb nil "(defmethod print-method sci.lang.IVar [o w] (.write w (str :foo (symbol o)))) (def x 1) (= \":foouser/x\" (pr-str #'x))"))))
(deftest reify-multiple-arities-test
(testing "ILookup"
(is (= ["->:foo" 10]
(bb nil "
(def m (reify clojure.lang.ILookup
(valAt [this x] (str \"->\" x))
(valAt [this x y] y)))
[(:foo m) (:foo m 10)]"))))
(testing "IFn"
(is (= [:yo :three :six :twelve :eighteen :nineteen 19]
(bb nil "
(def m (reify clojure.lang.IFn
(invoke [this] :yo)
(invoke [this _ _ _] :three)
(invoke [this _ _ _ _ _ _] :six)
(invoke [this _ _ _ _ _ _ _ _ _ _ _ _] :twelve)
(invoke [this _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _] :eighteen)
(invoke [this _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _] :nineteen)
(applyTo [this args] (last args))))
[
(m)
(m 1 2 3)
(m 1 2 3 4 5 6)
(m 1 2 3 4 5 6 1 2 3 4 5 6)
(m 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6)
(m 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1)
(apply m (range 20))
]")))))
;;;; Scratch
(comment