Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clojure, how can I undef a var from a namespace?

In clojure I have lines like this that define default values:

(def *http-port* 8080) 

I've now decided to formalize these kinds of values into a configuration unit and I would like to undefine the value *http-port* so that I can find the locations that still refer to this value and change them to use the new value. I'm doing a refactoring in other words by moving the value to a different location.

The way I've been doing this is to quit slime and try to restart the slime session. During maven's compile phase errors like these are picked up and I can find and fix one reference at a time. I then fix the error, wash rinse and repeat. This is obviously frustrating.

How would I do this while connected to a slime session?

like image 427
Pieter Breed Avatar asked Nov 17 '10 20:11

Pieter Breed


People also ask

What is Var in clojure?

Advertisements. In Clojure, variables are defined by the 'def' keyword. It's a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable.

What is a namespace in Clojure?

A namespace is both a name context and a container for vars. Namespace names are symbols where periods are used to separate namespace parts, such as clojure. string . By convention, namespace names are typically lower-case and use - to separate words, although this is not required.

What are the different types of namespaces in Clojure?

In require, namespaces most commonly take one of three forms: clojure.set - just loads clojure.set namespace (if not already loaded) In addition to vars, Clojure also provides support for Java interop and access to Java classes, which live in packages.

How do I declare dependencies in Clojure?

Most Clojure files represent a single namespace and declare the dependencies for that namespace at the top of the file using the ns macro, which often looks like this:

What is a variable in Clojure?

It’s a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable. One key thing to note in Clojure is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again.

How do I load code in Clojure?

There are several ways to load code in Clojure, but most commonly loading is accomplished via require. Due to this loading convention, most Clojure is structured with a 1-to-1 mapping of namespaces to files, stored in hierarchical fashion that maps to the namespace structure.


1 Answers

If I understand you correctly, ns-unmap should do what you want:

user=> foo java.lang.Exception: Unable to resolve symbol: foo in this context (NO_SOURCE_FILE:1) user=> (def foo 1) #'user/foo user=> foo 1 user=> (ns-unmap (find-ns 'user) 'foo) nil user=> foo java.lang.Exception: Unable to resolve symbol: foo in this context (NO_SOURCE_FILE:1) 
like image 73
Hugh Avatar answered Sep 25 '22 23:09

Hugh