简体   繁体   English

如何使用clojurescript repl评估宏

[英]how can macros be evaluated using the clojurescript repl

Once the browser is connected to the clojurescript repl, I previously had no way of calling macros from the repl. 一旦浏览器连接到clojurescript repl,我以前无法从repl调用宏。 This is an issue that has put me off clojurescript in the past, preferring using javascript directly. 这个问题在过去让我摆脱了clojurescript,更喜欢直接使用javascript。 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. 基本上,我觉得cljs-repl有点蹩脚,我回到编译/调试周期,在clojure中编写代码应该是从我们那里解放出来的。

Are there any good workarounds/workflows for pushing and testing code in clojurescript? 在clojurescript中推送和测试代码是否有任何良好的变通方法/工作流程? Especially if macros can be evaluated? 特别是如果可以评估宏?

An example of my problem is: 我的问题的一个例子是:

  1. make a new cljs project 制作一个新的cljs项目

    lein new cljs-template blah lein new cljs-template blah

  2. start up the server 启动服务器

    cd blah cd blah

    lein run 莱恩跑

  3. run the web-repl 运行web-repl

    lein trampoline cljsbuild repl-listen lein trampoline cljsbuild repl-listen

  4. there is a file src/blah/client/main.cljs with the heading 有一个带有标题的文件src/blah/client/main.cljs

(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]]) 注意这一行(:use-macros [crate.macros :only [defpartial]])

I can't use defpartial in the browser repl because it is a macro. 我不能在浏览器repl中使用defpartial ,因为它是一个宏。 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. 现在defpartial是一个非常有用的宏,没有它,这是一个麻烦。

My problem got worse when i wanted to define another macro in the project with the :use-macros . 当我想在项目中使用: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. 我无法在repl或浏览器中调试我写的内容,大约半天后,我发现使用clj repl更快,使用macroexpand测试宏并将结果复制回到浏览器代表。 I got one cljs macro working after about a day it wasn't very fun. 大约一天后,我有一个cljs宏工作,这不是很有趣。 This was about 6 months ago. 这是大约6个月前。 I'm hoping there is a quicker way to do this now. 我希望现在有更快的方法可以做到这一点。

In order for macros to be loaded during an interactive session w/ bREPL you need to explicitly evaluate the ns form in bREPL first. 为了在交互式会话期间加载宏w / bREPL,您需要首先在bREPL中明确评估ns表单。

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. 通过在启动时分析源文件,W还有一些关于使bREPL更有用的想法。

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. 今天我用cemerick / austin检查了一下 :一个clojureScript浏览器-REPL,您可以无限制地使用和评估brepl中的宏,也就是说没有先在bREPL中明确评估ns形式。 I'm using in this demo-project core.async macros and custom domain macros without problem. 我在这个演示项目 core.async宏和自定义域宏中使用没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM