Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a dynamic var in another namespace

Tags:

clojure

I am trying to define a dynamic var in a different namespace. The Lobos documentation states:

By default all migrations are kept in the lobos.migrations namespace. It'll get automatically loaded by migration commands, so there's no need to load it yourself. Thus, to use another namespace you must change the lobos.migration/migrations-namespace dynamic variable.

I can't figure out how to set the dynamic variable from within my new namespace.

I can do this in the repl via (ns `lobos.migration), but running this cmd from my own ns

(def ^:dynamic lobos.migration/*migrations-namespace* 'gb.install.migrations)

yields Can't create defs outside of current ns.

How can I fix this?

like image 645
Dustin Getz Avatar asked Jun 11 '12 19:06

Dustin Getz


1 Answers

Clojure vars can have a root binding that is visible to all threads. Additionally, dynamic vars can also have per-thread bindings, each visible to only one thread.

You can temporarily create a per-thread binding for your current thread by using binding:

(binding [lobos.migration/*migrations-namespace* 'gb.install.migrations]
  ;; binding is in effect here in the body of the binding form
  )

Or if a per-thread binding is already in effect, you can change its value using set!:

(set! lobos.migration/*migrations-namespace* 'gb.install.migrations)

But it's likely you'll need to change this particular dynamic var in a way visible across all threads. If this is true, you'll need to change its root binding by doing something like this:

(alter-var-root #'lobos.migration/*migrations-namespace*
                (constantly 'gb.install.migrations))

Note I don't know anything about lobos itself and so can't say with certainty that any of these will actually set the var in the way lobos desires.

like image 174
Chouser Avatar answered Nov 01 '22 10:11

Chouser