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

Query for enhancement: CLJS support for import-vars #31

Closed
davesann opened this issue Sep 14, 2014 · 6 comments
Closed

Query for enhancement: CLJS support for import-vars #31

davesann opened this issue Sep 14, 2014 · 6 comments

Comments

@davesann
Copy link

I wanted to use an equivalent of import-vars in ClojureScript (actually, in cljx code for both). I tried Potemkin on the off chance that the pre-compile macro expansion of ClojureScript would work out ok. It didn't.

I have written a couple of very basic macros which match the signature of import vars and work for vars and functions in ClojureScript .

Would you like to include something like this into Potemkin? If yes, I thought namespace potemkin.cljs.namespaces (.clj). I can post the code for your review (it's short) or create a pull request.

@ztellman
Copy link
Collaborator

I'd encourage you to create a separate library for ClojureScript, it honestly seems easier.

@davesann
Copy link
Author

no problem

@stephenbrady
Copy link

@davesann if you've managed to do this for vars and functions in cljs, would you mind publishing it? Looks like there would be some interest more broadly.

@davesann
Copy link
Author

@stephenbrady

I haven't looked at this in a while. But it worked fine for my purposes. Very basic macros as I mentioned. I haven't pushed this to github yet. It was too small... but you are free to use as you wish if it meets your needs.

(ns dsann.potemkin.cljs.namespaces)

(defmacro import-def 
  "import a single fn or var
   (import-def a b) => (def b a/b)
  "
  [from-ns def-name]
  (let [from-sym# (symbol (str from-ns) (str def-name))]
    `(def ~def-name ~from-sym#)))

(defmacro import-vars 
  "import multiple defs from multiple namespaces 
   works for vars and fns. not macros.
   (same syntax as potemkin.namespaces/import-vars)
   (import-vars 
     [m.n.ns1 a b]
     [x.y.ns2 d e f]) =>
   (def a m.n.ns1/a)
   (def b m.n.ns1/b)
    ...
   (def d m.n.ns2/d)
    ... etc
  "
  [& imports]
  (let [expanded-imports (for [[from-ns & defs] imports
                               d defs]
                           `(import-def ~from-ns ~d))]
  `(do ~@expanded-imports)))

@stephenbrady
Copy link

@davesann Thanks for sharing.

@ghost
Copy link

ghost commented Aug 2, 2016

@davesann thanks for sharing :)

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

3 participants