使用ClojureScript+Quil时遇No such namespace: quil.helpers.seqs错误的原因
为什么在ClojureScript中使用Quil时会出现"No such namespace: quil.helpers.seqs"错误?
我正在通过Quil学习ClojureScript,尝试将一个Quil示例(原示例为Clojure版本的随机涂鸦代码)用ClojureScript实现时,收到错误提示:No such namespace: quil.helpers.seqs。
复现步骤
- 执行Leiningen命令创建新项目:
lein new quil-cljs quil-helpers - 在项目的core.cljs文件中引入
quil.helpers.seqs命名空间并调用其函数,即可触发该错误
我的core.cljs文件内容
(ns quil-helpers.core (:require [quil.core :as q :include-macros true] [quil.helpers.seqs :as s] [quil.middleware :as m])) (defn draw [state] (s/range-incl 0 10) (q/background 200) (q/ellipse 200 200 100 100)) (defn ^:export run-sketch [] (q/defsketch quil-helpers :host "quil-helpers" :size [500 500] :draw draw :middleware [m/fun-mode]))
我的project.clj文件内容
(defproject quil-helpers "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.10.1"] [quil "3.1.0"] [org.clojure/clojurescript "1.10.520"]] :plugins [[lein-cljsbuild "1.1.7"] [lein-figwheel "0.5.19"]] :hooks [leiningen.cljsbuild] :clean-targets ^{:protect false} ["resources/public/js"] :cljsbuild {:builds [; development build with figwheel hot swap {:id "development" :source-paths ["src"] :figwheel true :compiler {:main "quil_helpers.core" :output-to "resources/public/js/main.js" :output-dir "resources/public/js/development" :asset-path "js/development"}} ; minified and bundled build for deployment {:id "optimized" :source-paths ["src"] :compiler {:main "quil_helpers.core" :output-to "resources/public/js/main.js" :output-dir "resources/public/js/optimized" :asset-path "js/optimized" :optimizations :advanced}}]})
问题原因及解决方法
问题出在Quil 3.x版本的ClojureScript实现中,已经移除了quil.helpers.seqs这个命名空间。你参考的示例是基于Clojure版本的Quil,而ClojureScript版本的Quil在3.x版本做了API精简,不再包含这个辅助命名空间。
解决方式:
- 自行实现所需函数:比如你用到的
range-incl可以自己简单实现:
(defn range-incl [start end] (range start (inc end)))
- 降级到Quil 2.x版本(不推荐):如果一定要使用原命名空间,可以在project.clj中将Quil依赖改为
[quil "2.8.0"],这个版本的ClojureScript实现仍包含quil.helpers.seqs,但旧版本不再维护,存在安全和兼容性风险。
内容的提问来源于stack exchange,提问作者Wizzard
相关产品推荐
相关产品推荐

