Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbound functions in clojure - How to bind them?

I am trying to deploy a topology on a storm server and it keeps telling me the following:

java.lang.RuntimeException: java.lang.IllegalStateException: Attempting to call unbound fn: #'storm-nblabla/operation-bolt__ at backtype.storm.clojure.ClojureBolt.prepare(ClojureBolt.java:60) a

So I guess I have to bind a function that I am using in the prepare function. So my question is How do you generally bind functions in clojure?

Regards,

Horace

like image 530
Horace Avatar asked Oct 25 '13 09:10

Horace


1 Answers

In clojure, a value is usually bound in one of two ways:

  • locals, without a namespace qualifier, (usually in a let statement or fn / loop args)
    This is for values that aren't referenced outside the scope of the block (except if they are provided as an argument to a function inside the block or in the return value of the block).

  • vars, with namespace scope, usually using def (or a secondary macro like defn)
    This is for values that should be accessible at namespace scope, which will be accessible wherever you can access the namespace.

The error (trying to call an unbound fn) is caused by using declare to create a var, and then calling it without providing a true definition:

user> (declare foo)
#'user/foo
user> (foo)
IllegalStateException Attempting to call unbound fn: #'user/foo  clojure.lang.Var$Unbound.throwArity (Var.java:43)

In this code the var exists (declare created it), but no value has been assigned. So you need the latter kind of binding, a var binding:

user> (defn foo [] "OK")
#'user/foo
user> (foo)
"OK"
user> 

Somewhere, some part of your code or the code of the library you are using has declared a var, which should be bound to a callable value, but has not been properly initialized. Does the library have an init function of some sort that you haven't called? Maybe there is a namespace you need to require before the definition is visible?

like image 176
noisesmith Avatar answered Oct 01 '22 17:10

noisesmith