Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we support a subset of proxy? #550

Closed
borkdude opened this issue Mar 12, 2021 · 2 comments
Closed

Can we support a subset of proxy? #550

borkdude opened this issue Mar 12, 2021 · 2 comments

Comments

@borkdude
Copy link
Collaborator

Goal: support a subset of proxy, e.g. proxy on APersistentMap like described in this blog post:

https://blog.wsscode.com/guide-to-custom-map-types/

/cc @GreshamDanielStephens @wilkerlucio

@borkdude
Copy link
Collaborator Author

borkdude commented Mar 12, 2021

A POC:

(defn proxy-fn [abstract-class methods]
  (case (.getName abstract-class)
    "clojure.lang.APersistentMap"
    (proxy
        [clojure.lang.APersistentMap]
        []
      (seq [] ((get methods 'seq)))
      (valAt ([k] ((get methods 'valAt) k))
        ([k d] ((get methods 'valAt) k d))))))

(def m (proxy-fn clojure.lang.APersistentMap
                 {'seq (fn []
                         (seq {:a 1 :b 2}))
                  'valAt (fn
                           ([k] k)
                           ([_k d] d))
                  'invoke (fn
                            ([k] k))}))

(prn (seq m)) ;; ([:a 1] [:b 2])
(prn (instance? clojure.lang.ILookup m)) ;; true
(prn (get m 1)) 1
(prn (instance? clojure.lang.IFn m)) ;; true
(prn (m 1)) ;; 1

borkdude added a commit that referenced this issue Mar 12, 2021
borkdude added a commit that referenced this issue Mar 12, 2021
borkdude added a commit that referenced this issue Mar 12, 2021
@borkdude
Copy link
Collaborator Author

borkdude added a commit that referenced this issue Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant