how can macros be evaluated using the clojurescript repl

半世苍凉 提交于 2019-12-07 11:17:32

问题


Once the browser is connected to the clojurescript repl, I previously had no way of calling macros from the repl. This is an issue that has put me off clojurescript in the past, preferring using javascript directly. Basically, I felt that the cljs-repl was kinda lame and I was going back to the compile/debug cycle that writing code in clojure was supposed to emancipate us from.

Are there any good workarounds/workflows for pushing and testing code in clojurescript? Especially if macros can be evaluated?

An example of my problem is:

  1. make a new cljs project

    lein new cljs-template blah

  2. start up the server

    cd blah

    lein run

  3. run the web-repl

    lein trampoline cljsbuild repl-listen

  4. there is a file src/blah/client/main.cljs with the heading

    (ns blad.client.main
      (:require [noir.cljs.client.watcher :as watcher]
                [clojure.browser.repl :as repl]
                [crate.core :as crate])
      (:use [jayq.core :only [$ append]])
      (:use-macros [crate.macros :only [defpartial]]))

notice the line (:use-macros [crate.macros :only [defpartial]])

I can't use defpartial in the browser repl because it is a macro. The error I get is:

>> (crate.macros/defpartial [])
"Error evaluating:" (crate.macros/defpartial []) :as "crate.macros.defpartial.call(null,cljs.core.Vector.fromArray([]));\n"
#
TypeError: Cannot read property 'defpartial' of undefined

Now defpartial is quite a useful macro and without it, it was a hassle.

My problem got worse when i wanted to define another macro in the project with the :use-macros. I could not debug what i wrote at all in the repl or the browser and after about half a day, I figured out that it was quicker to use a clj repl, test the macro in there using macroexpand and the copy the results back into the browser repl. I got one cljs macro working after about a day it wasn't very fun. This was about 6 months ago. I'm hoping there is a quicker way to do this now.


回答1:


In order for macros to be loaded during an interactive session w/ bREPL you need to explicitly evaluate the ns form in bREPL first.

Even so this is a bit annoying - some work has landed in master to support interactive macroexpansion but it needs more work. W also have a few ideas floating around about making bREPL more useful by doing analysis of the source files on startup.




回答2:


Today I have checked that with cemerick/austin: a clojureScript browser-REPL, you can use and evaluate your macros in the brepl without limitation, that's to say without explicitly evaluate the ns form in bREPL first. I'm using in this demo-project core.async macros and custom domain macros without problem.



来源:https://stackoverflow.com/questions/12337509/how-can-macros-be-evaluated-using-the-clojurescript-repl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!