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

macros #13

Open
nasser opened this issue Dec 24, 2016 · 0 comments
Open

macros #13

nasser opened this issue Dec 24, 2016 · 0 comments

Comments

@nasser
Copy link
Owner

nasser commented Dec 24, 2016

Macros are complicated by the fact that ClojureScript, for historic reasons, does not normally support macros. They are instead a compile-time feature used on the JVM side when generating JavaScript in the more conventional ClojureScript workflow. Making them work in a bootstrapped context is tricky.

The status of defmacro:

cljs.user=> (defmacro foo [x] `(+ ~x 90))
true

cljs.user=> (foo 9)
(cljs.core/+ nil 90)

This also makes consuming code from the ecosystem difficult. Consider this datascript snippet:

(ns datascript.btset
  ...
  (:refer-clojure :exclude [iter])
  (:require
    [datascript.arrays :as da])
  #?(:clj  (:import [java.util Arrays]))
  #?(:cljs (:require-macros [datascript.btset :refer [half not==]])))

#?(:clj
  (defmacro half [x]
    `(unsigned-bit-shift-right ~x 1)))

#?(:clj
  (defmacro not== [x y]
    `(not (== ~x ~y))))

It follows the common pattern of a namespace requiring itself to access macros. This fails to load with

cljs.user=> (require 'datascript.core)
#error {:message "Invalid :refer, macro datascript.btset/not== does not exist", :data {:tag :cljs/analysis-error}}
Error: Could not parse ns form datascript.btset
...

Ideas:

  • make defmacro work normally
  • intervene on the reader conditional and compile defmacros even though they are tagged :clj
  • intervene on ns form to turn require-macros into a normal require
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