Mason Stewart
Mason Stewart

Reputation: 868

How to expand macros in ClojureScript's cljs.core namespace

I was curious about what certain macros were doing and tried to call (macroexpand-1) to get more info. However, I'm a little confused about how to expand the built-in macros in ClojureScript, particularly the macros in the cljs.core namespace. According to the docs, ClojureScript macros are written in Clojure and therefore have to be tested in a Clojure REPL (instead of a ClojureScript REPL), which is where I've been trying this from.

Running lein repl from my ClojureScript project's directory, I've tried this:

=> (require 'cljs.compiler)
=> (require 'cljs.core)
=> (macroexpand-1 '(cljs.core/int 99.9))
(macroexpand-1 '(cljs.core/int 99.9))
(cljs.core/int 99.9)

Why does that return (cljs.core/int 99.9)? Based on the ClojureScript source, shouldn't that macro expand to something like (bit-or ~x 0)?

When I expand non-ClojureScript macros, such as (macroexpand-1 '(when (even? 2) (println "2 is even"))), the expansion seems to work fine.

Seems like I'm missing something conceptually...

Upvotes: 3

Views: 1882

Answers (2)

Michał Marczyk
Michał Marczyk

Reputation: 84379

Most likely you're using a ClojureScript version predating this commit which introduces the int macro. Try adding [org.clojure/clojurescript "0.0-1835"] to your :dependencies.

Also, while this is not relevant here, in general you should use ClojureScript's macroexpand-1 rather than Clojure's for tests like this one:

(require '[cljs.compiler :as comp]) ; must be required before cljs.core
(require '[cljs.core :as core])     ; the macros live here
(require '[cljs.analyzer :as ana])  ; macroexpand-1 lives here

;; ClojureScript's macroexpand-1 takes an environment as its first
;; argument; here's a useful initial environment:
(ana/macroexpand-1 {:locals {} :context :expr :ns 'cljs.user} '(int 5))
;= (cljs.core/bit-or 5 0)

Upvotes: 5

dnolen
dnolen

Reputation: 18556

There is no compiler macro for int is all.

Upvotes: 4

Related Questions