本文介绍了如何使用clojurescript repl评估宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦浏览器连接到clojurescript repl,我以前就无法从repl调用宏。这个问题过去使我无法使用clojurescript,而是更喜欢直接使用javascript。基本上,我觉得cljs-repl有点la脚,我回到了编译/调试周期,即用clojure编写代码应该可以使我们从中解放出来。

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.

是否有用于推送和测试clojurescript中的代码的良好解决方法/工作流程?

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. 创建一个新的cljs项目

  1. make a new cljs project


  • 启动服务器

  • start up the server

    lein run


  • 运行网络复制

  • run the web-repl


  • 有一个文件 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]]))
    

    注意该行(:use-macros [crate.macros :only [defpartial]])

    我不能在 defpartial 中使用浏览器repl,因为它是一个宏。我得到的错误是:

    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
    

    现在defpartial是一个非常有用的宏,没有它,这很麻烦。

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

    当我想使用:use-macros 在项目中定义另一个宏时,我的问题变得更加严重。我无法调试我在repl或浏览器中写的所有内容,大约半天后,我发现使用clj repl更快,在那里使用macroexpand测试宏,然后将结果复制回浏览器代表大约一天后,我得到了一个cljs宏,但这不是很有趣。这是大约6个月前。我希望现在可以有一种更快的方法。

    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.

    推荐答案

    以便在交互式会话期间加载宏使用bREPL,您需要先明确评估bREPL中的ns形式。

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

    即使如此,这也有点令人讨厌-有些工作已经落入master来支持交互式宏扩展,但还需要更多工作。关于启动时通过对源文件进行分析来使bREPL更有用的方法,W也有一些想法。

    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.

    这篇关于如何使用clojurescript repl评估宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-24 06:06