I have a small game that I'm writing in Clojure. I am very new to the language and one of the first errors I've stumbled upon is
Call to clojure.core/defn did not conform to spec.
I have no idea what causes the error, but an exception is thrown with this:
(ns survival-text.core
(:gen-class))
(def health 10)
(def hunger 10)
(def locations ["plains" "ocean" "mountains" "forest"])
(def currentLocation (atom 0))
(defn walk
(def random rand-int (count locations))
(reset! currentLocation (atom random)))
(defn choices
[input]
(case (input)
"walk" (walk)
"exit" (System/exit 0)
""))
(defn -main
[& args]
(while true
(def input read-line)
(choices input)))
But not this:
(ns survival-text.core
(:gen-class))
(def health 10)
(def hunger 10)
(def locations ["plains" "ocean" "mountains" "forest"])
(def currentLocation (atom 0))
(defn choices
[input]
(case (input)
"walk" (reset! currentLocation (atom (rand-nth locations)))
"exit" (System/exit 0)
""))
(defn -main
[& args]
(while true
(def input read-line)
(choices input)))
This is what is being thrown at me
Exception in thread "main" Syntax error macroexpanding clojure.core/defn at (survival_text/core.clj:9:1).
Call to clojure.core/defn did not conform to spec.
at clojure.lang.Compiler.checkSpecs(Compiler.java:6971)
at clojure.lang.Compiler.macroexpand1(Compiler.java:6987)
at clojure.lang.Compiler.macroexpand(Compiler.java:7074)
at clojure.lang.Compiler.eval(Compiler.java:7160)
at clojure.lang.Compiler.load(Compiler.java:7635)
at clojure.lang.RT.loadResourceScript(RT.java:381)
at clojure.lang.RT.loadResourceScript(RT.java:372)
at clojure.lang.RT.load(RT.java:463)
at clojure.lang.RT.load(RT.java:428)
at clojure.core$load$fn__6824.invoke(core.clj:6126)
at clojure.core$load.invokeStatic(core.clj:6125)
at clojure.core$load.doInvoke(core.clj:6109)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.core$load_one.invokeStatic(core.clj:5908)
at clojure.core$load_one.invoke(core.clj:5903)
at clojure.core$load_lib$fn__6765.invoke(core.clj:5948)
at clojure.core$load_lib.invokeStatic(core.clj:5947)
at clojure.core$load_lib.doInvoke(core.clj:5928)
at clojure.lang.RestFn.applyTo(RestFn.java:142)
at clojure.core$apply.invokeStatic(core.clj:667)
at clojure.core$load_libs.invokeStatic(core.clj:5985)
at clojure.core$load_libs.doInvoke(core.clj:5969)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.core$apply.invokeStatic(core.clj:667)
at clojure.core$require.invokeStatic(core.clj:6007)
at clojure.core$require.doInvoke(core.clj:6007)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at user$eval140$fn__144.invoke(form-init1707647033589713549.clj:1)
at user$eval140.invokeStatic(form-init1707647033589713549.clj:1)
at user$eval140.invoke(form-init1707647033589713549.clj:1)
at clojure.lang.Compiler.eval(Compiler.java:7176)
at clojure.lang.Compiler.eval(Compiler.java:7166)
at clojure.lang.Compiler.load(Compiler.java:7635)
at clojure.lang.Compiler.loadFile(Compiler.java:7573)
at clojure.main$load_script.invokeStatic(main.clj:452)
at clojure.main$init_opt.invokeStatic(main.clj:454)
at clojure.main$init_opt.invoke(main.clj:454)
at clojure.main$initialize.invokeStatic(main.clj:485)
at clojure.main$null_opt.invokeStatic(main.clj:519)
at clojure.main$null_opt.invoke(main.clj:516)
at clojure.main$main.invokeStatic(main.clj:598)
at clojure.main$main.doInvoke(main.clj:561)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:705)
at clojure.main.main(main.java:37)
Caused by: clojure.lang.ExceptionInfo: Call to clojure.core/defn did not conform to spec. {:clojure.spec.alpha/problems ({:path [:fn-tail :arity-1 :params], :pred clojure.core/vector?, :val (def random rand-int (count locations)), :via [:clojure.core.specs.alpha/defn-args :clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/param-list :clojure.core.specs.alpha/param-list], :in [1]} {:path [:fn-tail :arity-n :bodies :params], :pred clojure.core/vector?, :val def, :via [:clojure.core.specs.alpha/defn-args :clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/param-list :clojure.core.specs.alpha/param-list], :in [1 0]}), :clojure.spec.alpha/spec #object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x41e68d87 "clojure.spec.alpha$regex_spec_impl$reify__2509@41e68d87"], :clojure.spec.alpha/value (walk (def random rand-int (count locations)) (reset! currentLocation (atom random))), :clojure.spec.alpha/args (walk (def random rand-int (count locations)) (reset! currentLocation (atom random)))}
at clojure.spec.alpha$macroexpand_check.invokeStatic(alpha.clj:705)
at clojure.spec.alpha$macroexpand_check.invoke(alpha.clj:697)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.lang.Var.applyTo(Var.java:705)
at clojure.lang.Compiler.checkSpecs(Compiler.java:6969)
... 44 more
I am using leiningen and I haven't changed anything an all apart from the src/survival-text/core.clj
What causes the error and how do I fix it?
You forgot the args list. It should look like:
(defn walk
[] ; no args to this function
(let [random (rand-int (count locations))]
(reset! currentLocation random)))
You may also want to review Clojure intro material like Brave Clojure or Getting Clojure for an introduction. In particular, it is generally not a good idea to use (def ...)
inside of a function.