Interfacing Clojure and Clojurescript

人盡茶涼 提交于 2019-12-12 13:35:30

问题


I would like to make my existing Clojure libraries usable from clojurescript as well as create new applications which run in both Clojure and Clojurescript.

I understand Clojure and Clojurescript have their differences, and that there are metaprogramming solutions such as cljx. But, I have limited my Clojure code to a compatible subset of Clojurescript and I would simply like to call clojure from clojurescript (and possibly vice-versa). I have seen this possible in libraries such as core.match, but I am unable to replicate what is done there.

I am currently getting No such namespace, I am not sure if it is because it is not possible, or because my namespacing or directory structure is wrong, or some other reason.

For concreteness, I have a directory structure of the form

src/
  clj/
    clopjys/
      vector.clj
  cljs/
    cljdemo.cljs

I am trying to use vector.clj from within cljdemo.cljs, i.e:

(:require
  [clophys.vector :refer [coords std-uniform-vector]])

My project.clj has the following entries

  ...
  :source-paths ["src/clj"]
  :cljsbuild {
      :builds [{
          ; The path to the top-level ClojureScript source directory:
          :source-paths ["src/cljs"]
   ...

回答1:


You can't include clj code in cljs at runtime. The clj files will get included for the macros (which are defined in clj and is probably what you saw in core.match).

The best solution is cljx really, and if your code is actually compatible with clj and cljs then you will have a cljx file that will look exactly like a normal clj file. It is pretty simple and cool, try it out!




回答2:


Since this question has been written, Clojure reader conditionals have appeared in the core language (whereas .cljx is a library).



来源:https://stackoverflow.com/questions/23520882/interfacing-clojure-and-clojurescript

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