clojurehttp-kit

Using resolve to call function - Clojure


I am using resolve to call a function given they name (String), like

(call "+" 10 10 10) ;; 30

The call function:

(defn call [this & that]
  (apply (resolve (symbol this)) that))

Everything works fine on REPL. But when I start the project as a server using httpkit http server, I receive a NPE.

java.lang.NullPointerException
    at clojure.core$apply.invoke(core.clj:626)
    at tapahtuma.http_resources$call.doInvoke(http_resources.clj:41)
    at clojure.lang.RestFn.invoke(RestFn.java:423)
    at tapahtuma.http_resources$forwarder.invoke(http_resources.clj:61)
    at tapahtuma.http_resources$create_event.invoke(http_resources.clj:69)
    at compojure.response$eval1563$fn__1564.invoke(response.clj:26)
    at compojure.response$eval1524$fn__1525$G__1515__1532.invoke(response.clj:10)
    at compojure.core$make_route$fn__1699.invoke(core.clj:93)
    at compojure.core$if_route$fn__1683.invoke(core.clj:39)
    at compojure.core$if_method$fn__1676.invoke(core.clj:24)
    at compojure.core$routing$fn__1705.invoke(core.clj:106)
    at clojure.core$some.invoke(core.clj:2528)
    at compojure.core$routing.doInvoke(core.clj:106)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invoke(core.clj:628)
    at compojure.core$routes$fn__1709.invoke(core.clj:111)
    at tapahtuma.http_resources$wrap_content_type$fn__7192.invoke(http_resources.clj:22)
    at ring.middleware.cors$wrap_cors$fn__1800.invoke(cors.clj:47)
    at org.httpkit.server.HttpHandler.run(RingHandler.java:33)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

At line 41 of http_resources I have the call function.

I don't figured out what are happen here. Thanks for any help here. I created a project to help reproduce the problem


Solution

  • The following modification to your reproducer -- fully qualifying the namespace -- works properly:

    (defn call [this & that]
      (let [this-sym (symbol this)
            this-resolved (ns-resolve 'resolver-clj.core this-sym)]
         (.println System/out (str "current-ns: " *ns*))
         (.println System/out (str "this-sym: " this-sym))
         (.println System/out (str "this-resolved: " this-resolved))
         (apply this-resolved that)))
    

    The reason is obvious when you look at the value of *ns* in the reproduction environment, which is clojure.core.