-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#603] Partial support for reifying multiple classes
- Loading branch information
Showing
3 changed files
with
45 additions
and
62 deletions.
There are no files selected for viewing
Submodule sci
updated
from ea5a87 to 323a25
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,40 @@ | ||
(ns babashka.impl.reify | ||
{:no-doc true} | ||
#_(:require [clojure.math.combinatorics :as combo])) | ||
(:require [clojure.math.combinatorics :as combo])) | ||
|
||
;; (defmacro gen-reify-combos [classes] | ||
;; (let [subsets (rest (combo/subsets classes))] | ||
;; (reduce (fn [opts classes] | ||
;; (assoc opts (set (map (fn [[class _]] | ||
;; (list 'quote class)) | ||
;; classes)) | ||
;; ;; TODO: methods per class | ||
;; `(fn [{:keys [:methods]}] | ||
;; (reify | ||
;; ~@(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))) | ||
;; classes))))) | ||
;; {} | ||
;; subsets))) | ||
(set! *warn-on-reflection* false) | ||
|
||
;; (def combos (gen-reify-combos [[clojure.lang.IPending [[isRealized [this]]]] | ||
;; [clojure.lang.IDeref [[deref [this]]]]])) | ||
|
||
;; (def pending-and-deref | ||
;; (let [factory-fn (get combos '#{clojure.lang.IPending clojure.lang.IDeref})] | ||
;; (factory-fn {:methods {'clojure.lang.IPending {'isRealized (fn [_] false)} | ||
;; 'clojure.lang.IDeref {'deref (fn [_] :deref)}}}))) | ||
|
||
;; (prn @pending-and-deref) | ||
;; (prn (realized? pending-and-deref)) | ||
;; (prn (instance? clojure.lang.IDeref pending-and-deref)) | ||
;; (prn (instance? clojure.lang.IPending pending-and-deref)) | ||
|
||
;; (def only-deref | ||
;; (let [factory-fn (get combos '#{clojure.lang.IDeref})] | ||
;; (factory-fn {:methods {'clojure.lang.IDeref {'deref (fn [_] :deref)}}}))) | ||
|
||
;; (prn @only-deref) | ||
;; (prn (instance? clojure.lang.IDeref only-deref)) | ||
;; (prn (instance? clojure.lang.IPending only-deref)) | ||
(defmacro gen-reify-combos | ||
"Generates pre-compiled reify combinations" | ||
[methods] | ||
(let [subsets (rest (combo/subsets (seq methods)))] | ||
(reduce (fn [opts classes] | ||
(assoc opts | ||
(set (map (fn [[class _]] | ||
(list 'quote class)) | ||
classes)) | ||
(list 'fn ['methods] | ||
(list* 'reify | ||
(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))) | ||
classes))))) | ||
{} | ||
subsets))) | ||
|
||
#_:clj-kondo/ignore | ||
(def reify-opts | ||
{'java.nio.file.FileVisitor | ||
(fn [{:keys [:methods]}] | ||
{:obj (reify java.nio.file.FileVisitor | ||
(preVisitDirectory [this p attrs] | ||
((get methods 'preVisitDirectory) this p attrs)) | ||
(postVisitDirectory [this p attrs] | ||
((get methods 'postVisitDirectory) this p attrs)) | ||
(visitFile [this p attrs] | ||
((get methods 'visitFile) this p attrs)))}) | ||
'java.io.FileFilter | ||
(fn [{:keys [:methods]}] | ||
{:obj (reify java.io.FileFilter | ||
(accept [this f] | ||
((get methods 'accept) this f)))}) | ||
'java.io.FilenameFilter | ||
(fn [{:keys [:methods]}] | ||
{:obj (reify java.io.FilenameFilter | ||
(accept [this f s] | ||
((get methods 'accept) this f s)))})}) | ||
(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]}})) |
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