I'm attempting to create a new namespace in a new file for a Clojure project and when I try to compile I get the error "Exception in thread "main" java.lang.IllegalArgumentException: Parameter declaration missing, compiling:(algtone/algorithms/hilbert.clj:5:30)"
In file src/algtone/algorithms/hilbert.clj
(ns
^{:doc "Hilbert curve implementation"
:author "Me"
}
algtone.algorithms.hilbert)
Whats happen if you execute the code in a REPL? here everything works fine:
user=> (ns
#_=> ^{:doc "Hilbert curve implementation"
#_=> :author "Me"
#_=> }
#_=> algtone.algorithms.hilbert)
nil
algtone.algorithms.hilbert=>
Related
When I start a repl with lein repl I can run the function greet and it works as expected.
(ns var-test.core
(:gen-class))
(declare ^:dynamic x)
(defn greet []
(binding [x "Hello World."]
(println (load-string "x"))))
(defn -main [& args]
(greet))
But if run the code via lein run it fails with
java.lang.RuntimeException: Unable to resolve symbol: x in this context.
What am I missing?
Is the var x dropped during compilation, despite being declared, since it is never used outside of the string?
Edit:
Solution
#amalloy's comment helped me understand I need to bind *ns* in order load the string within the expected namespace, instead of a new, empty namespace.
This works as expected:
(ns var-test.core
(:gen-class))
(declare ^:dynamic x)
(defn greet []
(binding [x "Hello World."
*ns* (find-ns 'var-test.core)]
(println (load-string "x"))))
(defn -main [& args]
(greet))
Wow, I've never seen that function before!
According to the docs, load-string is meant to read & load forms one-at-a-time from an input string. Observe this code, made from my favorite template project:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require [tupelo.string :as str]))
(dotest
(def y "wilma")
(throws? (eval (quote y)))
(throws? (load-string "y"))
So it appears that load-string starts with a new, empty environment, then reads and evaluates forms one at a time in that new env. Since your x is not in that new environment, it can't be found and you get an error.
Try it another way:
(load-string
(str/quotes->double
"(def ^:dynamic x)
(binding [x 'fred']
(println :bb (load-string 'x'))) " ))
;=> :bb fred
In this case, we give all the code as text to load-string. It reads and eval's first the def, then the binding & nested load-string forms. Everything works as expected since the working environment contains the Var for x.
Some more code illustrates this:
(spy :cc
(load-string
"(def x 5)
x "))
with result
:cc => 5
So the eval produces the var x with value 5, then the reference to x causes the value 5 to be produced.
To my surprise, the partial load-string works in a fresh REPL:
demo.core=> (def x "fred")
#'demo.core/x
demo.core=> (load-string "x")
"fred"
So load-string must be coded to use any pre-existing
REPL environment as the base environment. When using lein run, there is no REPL environment available, so load-string starts with an empty environment.
I'm playing with the luminus guestbook app.
I added some logging statements in g.test.db.core.clj to look at the keys of the data structures. Look at "Expected Keys" and "Actual Keys" below.
(deftest test-messages
(jdbc/with-db-transaction [t-conn db/conn]
(jdbc/db-set-rollback-only! t-conn)
(let [timestamp (java.util.Date.)]
(is (= 1 (db/save-message!
{:name "Bobby"
:message "Hello World!"
:timestamp timestamp}
{:connection t-conn})))
(let [actual (-> (db/get-messages {} {:connection t-conn}))
expected {:name "Bobby"
:message "Hello World!"
:timestamp timestamp}]
(log/info "Expected Keys")
(pprint (keys expected))
(log/info "Actual Keys")
(pprint (keys actual)) ;;<--this is the problem
(is (= actual
expected))))))
The "Expected Keys" print fine, but I get a runtime exception for "Actual Keys":
[2017-03-04 14:31:28,076]Expected Keys
(:name :message :timestamp)
[2017-03-04 14:31:28,089]Actual Keys
lein test :only guestbook.test.db.core/test-messages
ERROR in (test-messages) (:) Uncaught exception, not in assertion.
expected: nil actual: java.lang.ClassCastException: null at [empty
stack trace]
lein test guestbook.test.handler
However, if I do this:
(pprint actual)
I get what I want:
({:id 35,
:name "Bobby",
:message "Hello World!",
:timestamp #inst "2017-03-04T04:31:01.030000000-00:00"})
What is going on? Why can't I print the keys from the data structure returned from the database?
It looks like actual is a list and not a map. Try (-> actual first keys pprint)
I'm writing tests for a Clojure app using Speclj. I'm accustomed in BDD to do things like this:
context "some context"
stub my-function :return true
it "has behavior one"
should true my-function
it "has behavior two"
should_not false my-function
But in Speclj I can't seem to find an example of how to share the stub across the characteristics, so I'm currently stuck writing code like this:
(describe "this"
(context "that"
(it "accepts nil"
(with-redefs [called-fn (constantly nil)]
(should= nil (my-fn nil)))))
(it "accepts 1"
(with-redefs [called-fn (constantly nil)]
(should= 100 (my-fn 1))))))
(I realize this is a somewhat contrived example and arguably those assertions could all go under one characteristic, but let's suppose for now that I have good reason to write the code like this.)
I want, however, to just have to stub called-fn once, but moving this up out of the its raises errors because the real called-fn gets called instead of my redef.
Is there a way to reuse redefs (or use Speclj stubs) in Speclj so I'm not stuck pushing them all down inside the characteristics?
You can use the around macro to accomplish this.
Here's an example spec:
(ns sample.core-spec
(:require [speclj.core :refer :all]
[sample.core :refer :all]))
(describe "a test"
(it "returns output from fn-a"
(should= 1 (fn-b)))
(describe "using with-redef"
(around [it] (with-redefs [fn-a (fn [] 2)] (it)))
(it "returns the output from redefined function"
(should= 2 (fn-b)))))
Source:
(ns sample.core)
(defn fn-a []
1)
(defn fn-b []
(fn-a))
I am adding Swagger annotations to JaxRs annotated services.
I have the following:
(^{
GET true
Path "/{who}"
ApiOperation {:value "Get a hello" :notes "simple clojure GET"}
Produces ["text/plain; charset=UTF-8"]
ApiResponses {:value [(ApiResponse {:code 200 :message "yay!"})]}
}
If I decompile the produced class the annotations look like this:
#ApiResponses({#com.wordnik.swagger.annotations.ApiResponse(code=200L, message="yay!")})
#Produces({"text/plain; charset=UTF-8"})
#ApiOperation(value="Get a hello", notes="simple clojure GET")
#Path("/{who}")
#GET(true)
notes that in the first annotation code = 200L
During runtime, this value must be an int, and I cannot figure out how to make this happen
if I try
ApiResponses {:value [(ApiResponse {:code (int 200) :message "yay!"})]}
I get a compilation error (using the maven swagger plugin)
Exception in thread "main" java.lang.ClassCastException: clojure.lang.Var cannot be cast to java.lang.Class, compiling:(pocclj/resourceclj.clj:14)
I have tried
(def success (int 200))
...
ApiResponses {:value [(ApiResponse {:code success :message "yay!"})]}
Which produces this compilation error:
Exception in thread "main" java.lang.IllegalArgumentException: Unsupported annotation value: success of class class java.lang.Integer, compiling:(pocclj/resourceclj.clj:14)
I have tried a bunch of other stuff (deref etc) but cant find the secret sauce.
I am fairly new to clojure and desperate for some help on this.
Thanks in advance
Martin
You are setting the type of ':code' correctly. Which can be tested independently:
user> (def something ^{:ApiResponses {:code (int 200) :message "yay!"}} {:some :data :goes :here})
#'user/something
user> (meta something)
{:ApiResponses {:code 200, :message "yay!"}}
user> (-> (meta something) :ApiResponses :code type)
java.lang.Integer
And without the cast the metadata contains the wrong type:
user> (def something-wrong ^{:ApiResponses {:code 200 :message "yay!"}} {:some :data :goes :here})
#'user/something-wrong
user> (meta something)
{:ApiResponses {:code 200, :message "yay!"}}
user> (-> (meta something-wrong) :ApiResponses :code type)
java.lang.Long
From the exception it looks like perhaps the call to ApiResponse is crashing. If ApiResponse is a macro that expects a number and not an s-expression, then I could see it not handling this properly. If it is a function you would need to look into why it is crashing.
If I provide a stub implementation for ApiResponse then It works for me:
user> (definterface Fooi (Something []))
user.Fooi
user> (def ApiResponse identity)
#'user/ApiResponse
user> (deftype Foo []
Fooi
(Something
^{GET true
Path "/{who}"
ApiOperation {:value "Get a hello" :notes "simple clojure GET"}
Produces ["text/plain; charset=UTF-8"]
ApiResponses {:value [(ApiResponse {:code (int 200) :message "yay!"})]}}
[this] (identity this)))
user.Foo
I don't know about ApiResponse, or much about annotations really, but: it looks like some macro (deftype?) is producing annotations for you, and you need it to see 200 as an int. Clojure doesn't have int literals, so the only way to hand an Integer object directly to a macro is through some other macro that calls it. It's not really possible to do this in a nice way; as far as I know you have to either use eval, or be very narrow by aiming specifically at int literals. Here's a sketch of a solution:
user> (use 'clojure.walk)
user> (defmacro with-int-literals [named-ints & body]
(prewalk-replace (into {}
(for [[k v] (partition 2 named-ints)]
[k (int v)]))
`(do ~#body)))
user> (map class (second (macroexpand-1 '(with-int-literals [x 100, y 200] [x y]))))
(java.lang.Integer java.lang.Integer)
So if you wrap your entire deftype (or whatever macro is generating these annotations) with a with-int-literals form, you can produce integers for it instead of longs. I don't actually know that this will work; perhaps there's something in the annotation processor that's fundamentally unable to handle ints for some reason. But at least this way you can offer it ints and hope for the best.
Edit
Since you actually need int literals in metadata, rather than in "ordinary" code, prewalk won't actually look at the data you care about. You'll have to write a version of walk that treats metadata in a sensible way, and then use it instead of prewalk here.
I try to write a macro in clojure that sets up a namespace and automatically adds a few methods to it. My macro wasn't working and I tracked it down to a do statement. It is not possible to declare a new namespace in a do and immediately afterwards declare a method in that namespace. Why?
This does not work:
(ns xyz)
(do
(ns abc)
(prn *ns*)
(defn tst[] (prn "test" *ns*)))
(prn "after" *ns*)
(tst)
This works (namespace declaration before the do):
(ns xyz)
(ns abc)
(do
(prn *ns*)
(defn tst[] (prn "test" *ns*)))
(prn "after" *ns*)
(tst)
Thanks for reading,
Markus
Actually your code happens to work with Clojure >1.0 but don't rely on that!
Each top level form is compiled and then executed:
(ns xyz) ; compiled with current ns / exec sets the current ns to xyz
(do ; the whole form is compiled inthe current ns (xyz) so it defines xyz/tst
(ns abc)
(prn *ns*)
(defn tst[] (prn "test" *ns*)))
; the form above is executed: it sets the new current ns
; and the value for xyz/tst
(prn "after" *ns*) ; we are in abc
(tst) ; abc/tst doesn't exists
In clojure > 1.0, a do a the top level is "unrolled" so your code is now equivalent to:
(ns xyz)
; (do
(ns abc)
(prn *ns*)
(defn tst[] (prn "test" *ns*));)
(prn "after" *ns*)
(tst)
Your code does not do what you probably think it does. The function tst
will print the value of *ns*, a var, at the time the function is run, not when it is defined.
user> (ns foobar)
nil
foobar> (abc/tst)
"test" #<Namespace foobar>
nil
foobar> (ns zelsbot)
nil
zelsbot> (abc/tst)
"test" #<Namespace zelsbot>
nil
What you are trying to do has already been nicely provided by clojure.contrib.with-ns:
(ns xyz
(:use clojure.contrib.with-ns))
(with-ns (create-ns 'abc)
(defn tst [] (print "defined in namespace abc")))
It evaluates its body in the namespace you provide as its first argument, thus allowing you to add functions to a namespace other than the current one.
Its a problem of "macro expansion time" time vs. "runtime". Do you want the code in the (do to happen when the program is compiled or when the program is run by the user?
the (ns ...) lines are expanded by the reader and then used by the compiler to decide where to put the resulting compiled code.
If i understand correctly ns sets the top level binding of the var that tells the rest of the compiler what name space it is building code for. As a though experiment try to think about what name space the (prn ...) expression should go in.