"No such var" while calling a (:require) 'd function

452 views
Skip to first unread message

Timothy Washington

unread,
Sep 4, 2011, 2:37:43 PM9/4/11
to clo...@googlegroups.com
Hi all, 

I have an annoying namespacing issue. I have a sort of command-wrapper that require's in all other commands fine: add, get, etc. But when I call (:require commands.remove), I get the error: 

$ lein test my-test 
    ... 
    clojure.lang.Var.applyTo (Var.java:518)
    clojure.main.main (main.java:37)
Caused by: java.lang.RuntimeException: No such var: commands/remove
    ...
    clojure.core$eval.invoke (core.clj:2745)
    bkell$remove.doInvoke (bkell.clj:61)
    clojure.lang.RestFn.invoke (RestFn.java:410)
    bkell_test/fn (bkell_test.clj:421)




Now, this is my require and invocation call. These calls works fine for 4 other require / invocations. So I'm confused as to why it would fail now. 

(ns bkell 

  (:import java.io.FileReader)
  (:require commands.add)
  (:require commands.update)
  (:require commands.get)
  (:require commands.remove)
  (:require commands.authenticate)
  (:require domain)
  (:require util)
)
...

(defn remove [akey & etal]
  (let [  logged-in-user (commands/logged-in-user)]
    (if (-> logged-in-user nil?)  ;; we want to see a logged-in-user 
      (util/generate-error-response "User is not authenticated")
      (eval `(commands/remove ~akey ~@etal))     ;; this is line 61 
    )   
  )
)


Thanks for any insight 

Tim Washington 

Sean Corfield

unread,
Sep 4, 2011, 3:56:19 PM9/4/11
to clo...@googlegroups.com
On Sun, Sep 4, 2011 at 11:37 AM, Timothy Washington <twas...@gmail.com> wrote:
> (ns bkell
>   (:import java.io.FileReader)
>   (:require commands.add)
>   (:require commands.update)
>   (:require commands.get)
>   (:require commands.remove)
>   (:require commands.authenticate)
>   (:require domain)
>   (:require util)
> )

These are requiring namespaces so I would expect you'd have a
namespace commands.remove and it would contain functions...?

> (defn remove [akey & etal]
>   (let [  logged-in-user (commands/logged-in-user)]
>     (if (-> logged-in-user nil?)  ;; we want to see a logged-in-user
>       (util/generate-error-response "User is not authenticated")
>       (eval `(commands/remove ~akey ~@etal))     ;; this is line 61
>     )
>   )
> )

I would expect to see (commands.remove/some-func ..) here... so I'm a
little puzzled when you say it works for invoking functions in other
require'd commands.* namespaces. Can you share a bit more of your
code?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Timothy Washington

unread,
Sep 4, 2011, 4:20:19 PM9/4/11
to clo...@googlegroups.com
Hey, thanks for getting back to me. 

I was hacking away at this at the meantime. I defined 'remove' functions in 2 other places - bkell , commands. I ended up renaming commands/remove to commands/removek, and now all invocations are working. But I'm pretty sure I'm using namespace and require erroneously. Currently, I have a source file structure like below: 

src/
   bkell.clj 
src/commands/ 
   add.clj 
   remove.clj 
   etc... 


But each of the 'src/commands/' clojure files, uses the '(ns command ...)' namespace, not '(ns command.remove ...)', etc. I wanted to do it this way, so that client code could just call command/remove, not command.remove/remove. But I'm realising that I should instead define (ns commands) once, and use (in-ns) in other locations. You can see the source here. Looks like 'get' can be redefined in two other namespaces: bkell , commands. But when I try to do that for 'remove', the repl seizes up, and I'm curious to know why. 

Thanks for the feedback 
Tim 



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Sean Corfield

unread,
Sep 4, 2011, 4:50:39 PM9/4/11
to clo...@googlegroups.com
On Sun, Sep 4, 2011 at 1:20 PM, Timothy Washington <twas...@gmail.com> wrote:
> I was hacking away at this at the meantime. I defined 'remove' functions in
> 2 other places - bkell , commands. I ended up renaming commands/remove to
> commands/removek, and now all invocations are working. But I'm pretty sure
> I'm using namespace and require erroneously. Currently, I have a source file
> structure like below:
> src/
>    bkell.clj
> src/commands/
>    add.clj
>    remove.clj
>    etc...
>
> But each of the 'src/commands/' clojure files, uses the '(ns command ...)'
> namespace, not '(ns command.remove ...)', etc. I wanted to do it this way,
> so that client code could just call command/remove, not
> command.remove/remove. But I'm realising that I should instead define (ns
> commands) once, and use (in-ns) in other locations. You can see the source
> here. Looks like 'get' can be redefined in two other namespaces: bkell ,
> commands. But when I try to do that for 'remove', the repl seizes up, and
> I'm curious to know why.

The require should be:

(ns bkell
(:require commands))

and then you'll reference the functions as (commands/add ...)
(commands/remove ...) etc.

I'm not sure why (:require commands.remove) doesn't give you an
error... perhaps it was pulling in commands/remove.clj anyway?

Is there any reason why you have the commands split across multiple
files? (I'm not saying it's bad, just curious)

I suspect you're somehow calling remove recursively (but it's hard to
tell without seeing more of your code).

Sounds like you're gradually getting a handle on namespaces...

Timothy Washington

unread,
Sep 4, 2011, 5:06:41 PM9/4/11
to clo...@googlegroups.com
That's interesting. When I try that require A), I get the error in B). Seems that it's trying to find the commands.clj file. 

A) 
ns bkell
  (:import java.io.FileReader)
  (:require domain)
  (:require util)
  (:require commands)
)

B) 
Exception in thread "main" java.lang.RuntimeException: java.io.FileNotFoundException: Could not locate commands__init.class or commands.clj on classpath: 


You can indeed look at the bkell.clj and commands/ source here: 

Thanks Sean 
Tim 


Sean Corfield

unread,
Sep 4, 2011, 6:46:32 PM9/4/11
to clo...@googlegroups.com
On Sun, Sep 4, 2011 at 2:06 PM, Timothy Washington <twas...@gmail.com> wrote:
> That's interesting. When I try that require A), I get the error in B). Seems
> that it's trying to find the commands.clj file.

Yes. If you look at how various projects work with multiple files in a
single namespace, I think they have a file for the namespace that then
loads the others (which are part of that namespace). See this SO Q&A
for an example:

http://stackoverflow.com/questions/4690758/splitting-a-clojure-namespace-over-multiple-files

Timothy Washington

unread,
Sep 4, 2011, 7:24:56 PM9/4/11
to clo...@googlegroups.com
Oh nice one. I'm going to finish this sprint, then do a second pass over this code base and refactor this and a few other issues. 

Cheers 
Tim 


Reply all
Reply to author
Forward
0 new messages